Go

Go Basics

Variables, constants, basic types, functions, packages, imports, control flow, defer

25 interview questionsยท
Junior
1

What is the correct syntax to declare a variable in Go?

Answer

In Go, there are three main ways to declare a variable: var x int (explicit declaration with type), var x = 10 (declaration with type inference), and x := 10 (short declaration with inference, only inside functions). The short form := is most commonly used for local variables as it is concise and idiomatic.

2

What is the zero value of an int variable in Go?

Answer

In Go, all variables are initialized with a zero value by default if no value is provided during declaration. For numeric types (int, float64), the zero value is 0. For booleans it's false, for strings it's an empty string, and for pointers it's nil. This behavior ensures there are never uninitialized variables in Go.

3

What is the difference between var x int and x := 0 in Go?

Answer

var x int declares a variable with zero value (0) and an explicit type, while x := 0 uses short declaration with type inference. The short declaration := can only be used inside functions, while var can be used at package level. The := form is preferred for local variables as it is more concise and idiomatic.

4

How to declare a constant in Go?

5

What are the basic numeric types in Go?

+22 interview questions

Master Go for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free