Challenge - 5 Problems
Go Zero Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of uninitialized variables
What is the output of this Go program?
Go
package main import "fmt" func main() { var a int var b string var c bool fmt.Printf("%d %q %t", a, b, c) }
Attempts:
2 left
๐ก Hint
Think about what Go assigns to variables when you don't set them.
โ Incorrect
In Go, uninitialized variables get their zero values. For int it's 0, for string it's an empty string "", and for bool it's false.
โ Predict Output
intermediate2:00remaining
Zero value of a slice
What will this Go program print?
Go
package main import "fmt" func main() { var s []int fmt.Println(s == nil) fmt.Println(len(s)) }
Attempts:
2 left
๐ก Hint
Slices have a zero value of nil and length zero.
โ Incorrect
The zero value of a slice is nil. So s == nil is true. The length of a nil slice is 0.
โ Predict Output
advanced2:00remaining
Zero value of a map and its behavior
What is the output of this Go program?
Go
package main import "fmt" func main() { var m map[string]int fmt.Println(m == nil) fmt.Println(m["key"]) m["key"] = 10 }
Attempts:
2 left
๐ก Hint
Maps have zero value nil, reading from nil map is allowed, writing causes panic.
โ Incorrect
The zero value of a map is nil. Reading from a nil map returns the zero value of the element type (0 here). But writing to a nil map causes a runtime panic.
โ Predict Output
advanced2:00remaining
Zero value of a struct with embedded fields
What will this Go program print?
Go
package main import "fmt" type Inner struct { X int } type Outer struct { Inner Y string } func main() { var o Outer fmt.Printf("%d %q", o.X, o.Y) }
Attempts:
2 left
๐ก Hint
Embedded structs get zero values too.
โ Incorrect
The embedded struct Inner is zero-initialized, so o.X is 0. The string Y is empty string "".
โ Predict Output
expert2:00remaining
Zero value of interface and type assertion
What is the output of this Go program?
Go
package main import "fmt" func main() { var i interface{} fmt.Println(i == nil) v, ok := i.(int) fmt.Println(v, ok) }
Attempts:
2 left
๐ก Hint
An uninitialized interface is nil; type assertion fails safely.
โ Incorrect
The zero value of an interface is nil. So i == nil is true. The type assertion i.(int) fails, so ok is false and v is zero value of int (0).