Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Comparison Questions - Part 1
GO

Comparison Questions - Part 1

Practice Go comparison questions covering ==, strings, runes, nil versus empty slices, and pointer comparisons.

1. What does this == comparison print?

func main() {
	a := 5
	b := 5
	c := 6
	fmt.Println(a == b)
	fmt.Println(a == c)
	fmt.Println(a < c)
}

Output:

true
false
true

5 == 5true, 5 == 6false, 5 < 6true. Numeric comparison in Go is value-based and always well-defined.

2. What does this == on strings print?

func main() {
	s1 := "abc"
	s2 := "abc"
	s3 := "abd"
	fmt.Println(s1 == s2)
	fmt.Println(s1 == s3)
	fmt.Println(s1 < s3)
}

Output:

true
false
true

Strings compare by content, lexicographically (byte by byte). "abc" == "abc"true. "abc" < "abd"true since 'c' < 'd'. String == compares values, not references — there is no reference equality in Go for strings.

3. What does this == on []byte vs string print?

func main() {
	b := []byte("hello")
	s := "hello"
	fmt.Println(string(b) == s)
	fmt.Println(b == nil)
	fmt.Println(len(b))
}

Output:

true
false
5

A []byte isn’t directly comparable to a string — string(b) == s converts first → true. b == nil is false (it’s a non-nil slice). len(b) is 5.

4. What does this == on two []byte values print?

func main() {
	a := []byte("hi")
	b := []byte("hi")
	fmt.Println(a == nil)
	fmt.Println(string(a) == string(b))
	// fmt.Println(a == b) // compile error: slices can only be compared to nil
}

Output:

false
true

Slices can only be compared to nila == b doesn’t compile. So compare their string forms: string(a) == string(b)true. This is why bytes.Equal(a, b) exists. The commented line is the compile error.

5. What does this == on runes print?

func main() {
	fmt.Println('a' == 'a')
	fmt.Println('a' == 'A')
	fmt.Println('a' < 'b')
	fmt.Println('A' < 'a')
}

Output:

true
false
true
true

Runes are integer code points. 'a' and 'A' differ (97 vs 65). 'a' < 'b' since 97 < 98. 'A' < 'a' since uppercase sorts before lowercase. Character comparison is numeric comparison.

6. What does this == on a missing map key print?

func main() {
	m := map[string]int{}
	fmt.Println(m["x"] == 0)
	m["x"] = 0
	fmt.Println(m["x"] == 0)
}

Output:

true
true

Reading a missing key returns the zero value 0, so m["x"] == 0 is true before and after inserting 0. == can’t tell you whether a key exists — use the comma-ok form (v, ok := m["x"]) instead.

7. What does this == on float values print?

func main() {
	a := 0.5
	b := 0.5
	x := 0.1
	y := 0.2
	z := 0.3
	p := 0.3
	q := 0.2
	r := 0.1
	fmt.Println(a == b)
	fmt.Println(x+y == z)
	fmt.Println(p-q == r)
}

Output:

true
false
false

With variables, these are runtime float64 computations. 0.5 == 0.5true (exactly representable). x + y is 0.30000000000000004, not zfalse. 0.3 - 0.2 is 0.09999999999999998, not rfalse. (As untyped constants, 0.1+0.2 == 0.3 would be true — constants compute exactly.) Floats rarely compare equal with ==; use an epsilon.

8. What does this == on interface values print?

func main() {
	var a interface{} = 1
	var b interface{} = 1
	var c interface{} = int64(1)
	fmt.Println(a == b)
	fmt.Println(a == c)
}

Output:

true
false

a and b both hold int(1)true. c holds int64(1) — a different dynamic type — so a == c is false even though the numeric value matches. Interface equality requires the same dynamic type and value.

9. What does this == on a typed-nil interface print?

func main() {
	var p *int
	var i interface{} = p
	fmt.Println(p == nil)
	fmt.Println(i == nil)
}

Output:

true
false

p is a nil *intp == nil is true. But assigning it to an interface wraps it in a non-nil interface: the dynamic type is set (*int) even though the value is nil, so i == nil is false. The classic “typed nil” trap.

10. What does this == on structs print?

type Point struct{ x, y int }

func main() {
	p1 := Point{1, 2}
	p2 := Point{1, 2}
	p3 := Point{1, 3}
	fmt.Println(p1 == p2)
	fmt.Println(p1 == p3)
}

Output:

true
false

Structs whose fields are all comparable are comparable themselves. p1 and p2 have identical fields → true. p3 differs in yfalse. This is structural equality, not reference equality.

11. What does this == on arrays print?

func main() {
	a1 := [3]int{1, 2, 3}
	a2 := [3]int{1, 2, 3}
	a3 := [3]int{1, 2, 4}
	fmt.Println(a1 == a2)
	fmt.Println(a1 == a3)
}

Output:

true
false

Arrays are comparable element by element. a1 == a2true, a1 == a3false. Unlike slices, arrays support == directly because their length is part of the type.

12. What does this == on a slice vs nil print?

func main() {
	var s []int
	e := []int{}
	fmt.Println(s == nil)
	fmt.Println(e == nil)
	fmt.Println(len(s) == len(e))
}

Output:

true
false
true

var s []int declares a nil slice, so s == niltrue. e := []int{} is a non-nil empty slice, so e == nilfalse. Both have len 0, so len(s) == len(e)true. Nil and empty are different things in Go — and slices can only be compared to nil, not to each other.

13. What does this == on string bytes print?

func main() {
	fmt.Println("hello"[0] == 'h')
	fmt.Println("hello"[0] == "hello"[4])
	fmt.Println("hello" < "help")
}

Output:

true
false
true

"hello"[0] is byte 'h'true. 'h' (104) vs 'o' (111) → false. Lexicographic: "hello" and "help" share "hel", then 'l' < 'p'true. Indexing a string yields a byte (a uint8).

14. What does this == on pointers print?

func main() {
	a := 10
	b := 10
	pa := &a
	pb := &b
	fmt.Println(pa == pb)
	fmt.Println(*pa == *pb)
}

Output:

false
true

pa == pb compares addressesa and b are distinct variables → false. *pa == *pb compares the values (10 == 10) → true. Pointers compare by address; dereference to compare values.

15. What does this == on equal pointers print?

func main() {
	a := 10
	pa := &a
	pb := &a
	fmt.Println(pa == pb)
	fmt.Println(*pa == *pb)
}

Output:

true
true

Both pointers hold &a, so pa == pbtrue. Dereferenced, both are 10true. Pointers to the same variable are equal; two & of the same variable compare equal.

My Private Notes

Notes are auto-saved locally to this device.