16. What does this == on map keys print?
func main() {
m := map[string]int{"a": 1}
_, ok1 := m["a"]
_, ok2 := m["b"]
fmt.Println(ok1 == ok2)
fmt.Println(m["a"] == m["b"])
}
Output:
false
false
ok1 is true ("a" exists), ok2 is false ("b" doesn’t) → true == false is false. m["a"] is 1, m["b"] is the zero value 0 → 1 == 0 is false. The comma-ok form is how you compare key existence.
17. What does this == on string length print?
func main() {
s1 := "go"
s2 := "golang"
fmt.Println(len(s1) == len(s2))
fmt.Println(len(s1) < len(s2))
fmt.Println(s1 == s2[:2])
}
Output:
false
true
true
len("go") is 2, len("golang") is 6 → 2 == 6 is false, 2 < 6 is true. s2[:2] is "go", equal to s1 → true.
18. What does this == on channel identity print?
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
var ch3 chan int
fmt.Println(ch1 == ch2)
fmt.Println(ch1 == ch1)
fmt.Println(ch3 == nil)
}
Output:
false
true
true
Channels are comparable by identity (reference). Two make calls produce distinct channels → false. ch1 == ch1 is true. ch3 is a nil channel → ch3 == nil is true. A channel never equals another channel unless it’s the same channel (or both nil).
19. What does this == on function identity print?
func f() {}
func g() {}
func main() {
var h func()
fmt.Println(h == nil)
// fmt.Println(f == g) // compile error: functions can only be compared to nil
}
Output:
true
h is a nil function value, so h == nil → true. Function values (like slices and maps) can only be compared to nil — f == g doesn’t compile, which is why the commented line stays commented. There’s no function equality in Go.
20. What does this == on rune vs int print?
func main() {
var r rune = 'A'
var i int = 65
fmt.Println(int(r) == i)
fmt.Println(r == 'A')
fmt.Println(r == rune(i))
}
Output:
true
true
true
rune is an alias for int32, which is a distinct type from int — r == i doesn’t even compile. You must convert: int(r) == i → true. r == 'A' compares two runes → true. r == rune(i) → true. Go is strict about named types.
21. What does this == on multiple assignment print?
func main() {
x, y := 10, 20
fmt.Println(x == y)
x, y = y, x
fmt.Println(x == y)
}
Output:
false
false
Initially 10 == 20 → false. After the parallel swap, x = 20, y = 10, so 20 == 10 → false. The swap exchanges the values; the equality of the pair doesn’t change. To make them equal you’d assign the same value to both.
22. What does this == on empty vs nil string print?
func main() {
var s string
t := ""
fmt.Println(s == t)
fmt.Println(s == " ")
fmt.Println(s == "")
}
Output:
true
false
true
The zero value of a string is "", so s == t → true and s == "" → true. s == " " compares empty against a single space → false. Strings have no nil state; var s string is immediately "".
23. What does this == on bool values print?
func main() {
var b bool
fmt.Println(b == false)
fmt.Println(b == true)
b = !b
fmt.Println(b == true)
}
Output:
true
false
true
The zero value of bool is false, so b == false → true, b == true → false. After b = !b, b is true → true. Booleans are comparable and their zero value is false.
24. What does this == on composite literals print?
type Pair struct{ a, b int }
func main() {
p1 := Pair{1, 2}
p2 := Pair{1, 2}
m1 := map[string]int{"x": 1}
m2 := map[string]int{"x": 1}
fmt.Println(p1 == p2)
fmt.Println(m1 == nil)
fmt.Println(m2 == nil)
}
Output:
true
false
false
Structs compare structurally → p1 == p2 is true. Maps can only compare to nil; both m1 and m2 are non-nil (created with literals) → both == nil are false. You can’t m1 == m2 in Go.
25. What does this == on interface type switch print?
func main() {
var i interface{} = 42
v, ok := i.(int)
fmt.Println(v == 42)
fmt.Println(ok == true)
}
Output:
true
true
i.(int) type-asserts; v is 42, ok is true. So v == 42 → true and ok == true → true. Type assertions return the value and whether it matched.
26. What does this == on rune slices print?
func main() {
r1 := []rune("hi")
r2 := []rune("hi")
fmt.Println(r1 == nil)
fmt.Println(string(r1) == string(r2))
}
Output:
false
true
r1 is a non-nil slice → == nil is false. Slices aren’t directly comparable, but string(r1) == string(r2) compares the decoded text → true. []rune holds Unicode code points; string() re-encodes them.
27. What does this == on same underlying array print?
func main() {
arr := [4]int{1, 2, 3, 4}
s1 := arr[1:3]
s2 := arr[1:3]
s3 := arr[2:4]
fmt.Println(s1[0] == s2[0])
fmt.Println(s1[0] == s3[0])
}
Output:
true
false
s1[0] and s2[0] both read arr[1] = 2 → true. s3[0] reads arr[2] = 3 → 2 == 3 is false. Slices sharing a backing array still compare element by element.
28. What does this == on struct with pointer field print?
type Node struct {
val int
next *Node
}
func main() {
a := Node{1, nil}
b := Node{1, nil}
fmt.Println(a == b)
fmt.Println(a.next == b.next)
}
Output:
true
true
Node contains comparable fields (int and a pointer), so Node is comparable. a == b compares both fields: 1 == 1 and nil == nil → true. Pointer fields compare by address.
29. What does this == on string concatenation print?
func main() {
a := "foo"
b := "bar"
c := a + b
fmt.Println(c == "foobar")
fmt.Println(a + b == b + a)
}
Output:
true
false
a + b is "foobar" → true. "foobar" == "barfoo" → false. String concatenation produces a new string; equality is content-based.
30. What does this == on loop counter print?
func main() {
count := 0
for i := 1; i <= 5; i++ {
if i%2 == 0 {
count++
}
}
fmt.Println(count == 2)
}
Output:
true
The loop counts even numbers from 1 to 5: 2 and 4 → count is 2, so count == 2 → true. i%2 == 0 is true exactly for the two even values.
Premium Content
Unlock Comparison Questions - Part 2 and all premium lessons with a subscription.
From ₹199.99/year — See plans