WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Go · Basic · question 4 of 100

What is a zero value in Go and can you give an example?

📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

A zero value in Go is the default value automatically assigned to a variable of any type when it is declared but not yet initialized. Zero values for different data types might differ. For example, the zero value for a ‘bool‘ variable would be ‘false‘, and for an ‘int‘ variable, it would be ‘0‘. The zero value for a pointer is ‘nil‘.

Let me provide you some examples for different data types:

1. Boolean (‘bool‘): The zero value is ‘false‘

var flag bool

At this point, ‘flag‘ has the zero value, which is ‘false‘ for a ‘bool‘.

2. Integer (‘int‘, ‘int8‘, ‘int16‘, ‘int32‘, ‘int64‘): The zero value is ‘0‘

var count int

At this point, ‘count‘ has the zero value, which is ‘0‘ for an ‘int‘.

3. Floating-point numbers (‘float32‘, ‘float64‘): The zero value is ‘0.0‘

var distance float64

At this point, ‘distance‘ has the zero value, which is ‘0.0‘ for a ‘float64‘.

4. Complex numbers (‘complex64‘, ‘complex128‘): The zero value is ‘0 + 0i‘

var complexNumber complex128

At this point, ‘complexNumber‘ has the zero value, which is ‘0 + 0i‘ for a ‘complex128‘.

5. Strings (‘string‘): The zero value is the empty string ‘""`

var message string

At this point, ‘message‘ has the zero value, which is the empty string (‘""`) for a ‘string‘.

6. Pointers: The zero value is ‘nil‘

type Person struct {
    Name string
}

var personPtr *Person

At this point, ‘personPtr‘ has the zero value, which is ‘nil‘ for any pointer.

7. Arrays and slices:

var arr [5]int // array of 5 integers

Here, the ‘arr‘ would be an array of zeroes (‘0, 0, 0, 0, 0‘) since its element type is ‘int‘ and the zero value for ‘int‘ is ‘0‘.

var mySlice []int // slice of integers

For slices, the zero value is ‘nil‘, indicating an empty slice. So, ‘mySlice‘ would be ‘nil‘.

8. Maps: The zero value is ‘nil‘

var myMap map[string]int

At this point, ‘myMap‘ has the zero value, which is ‘nil‘ for maps.

9. Channels: The zero value is ‘nil‘

var ch chan string

At this point, ‘ch‘ has the zero value, which is ‘nil‘ for channels.

10. Functions: The zero value is ‘nil‘

type MyFunc func(int) string

var myFunction MyFunc

At this point, ‘myFunction‘ has the zero value, which is ‘nil‘ for functions.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Go interview — then scores it.
📞 Practice Go — free 15 min
📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

All 100 Go questions · All topics