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:

    go Copy
    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:

    go Copy
    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:

    go Copy
    var a, ...
junior

junior

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

middle

How to initialise a struct in Go?

middle

Why would you prefer to use an empty struct{} ?

middle

How do you swap two values? Provide a few examples.

Bình luận

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

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