What is static type declaration of a variable in Go?
What is static type declaration of a variable in Go?
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:
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
.
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.
Declaring multiple variables of the same type in a single statement:
var a, ...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào