Câu hỏi phỏng vấn Golang
Câu hỏi

What is static type declaration of a variable in Go?

Câu trả lời

In Go, a static type declaration of a variable explicitly specifies the type of the variable at the time of its declaration. This type declaration ensures that the variable is bound to a specific type and the compiler is aware of this type, which helps in type checking during compile time. Static type declaration is crucial for maintaining type safety within the code.

Here are the common ways to declare a variable with a static type in Go:

  1. Using the var keyword followed by the variable name, the type, and an optional initializer:

    var x int = 10

    In this example, x is explicitly declared as an int.

  2. If the initializer is provided, Go can infer the type of the variable, so you can omit the type:

    var y = 10

    Here, y is still statically typed as int because the literal 10 is an integer, and Go uses type inference.

  3. Declaring multiple variables of the same type in a single statement:

    var a, ...
junior

junior

Gợi ý câu hỏi phỏng vấn

middle

How to check if a Map contains a key in Go?

junior

What are Goroutines?

middle

What would you do if you need a hash displayed in a fixed order?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào