What is the output of this Go program?
package main import "fmt" type Counter struct { count int } func (c Counter) Increment() { c.count++ } func (c *Counter) IncrementPtr() { c.count++ } func main() { c := Counter{count: 5} c.Increment() fmt.Println(c.count) c.IncrementPtr() fmt.Println(c.count) }
Think about whether the method receiver is a value or a pointer and how it affects the original struct.
The method Increment has a value receiver, so it works on a copy of the struct and does not change the original count. The method IncrementPtr has a pointer receiver, so it modifies the original struct's count. Therefore, the first print shows 5, and the second shows 6.
What will this Go program print?
package main import "fmt" type Node struct { value int next *Node } func (n *Node) PrintValue() { if n == nil { fmt.Println("nil node") return } fmt.Println(n.value) } func main() { var n *Node = nil n.PrintValue() }
Check how method calls on nil pointers behave when the method has a pointer receiver.
In Go, calling a method with a pointer receiver on a nil pointer is allowed as long as the method itself does not dereference the pointer without checking. Here, the method checks if n == nil before accessing n.value, so it prints "nil node" safely.
Consider this Go code snippet. Why does calling p.Increment() cause a compilation error?
package main import "fmt" type Point struct { x, y int } func (p *Point) Increment() { p.x++ p.y++ } func main() { var p Point p.Increment() fmt.Println(p) }
Think about how Go handles method calls with pointer receivers on values.
In Go, methods with pointer receivers can be called on values because the compiler automatically takes the address. So this code actually compiles and runs fine, printing {1 1}. Therefore, option A is correct.
What is the output of this Go program?
package main import "fmt" type Printer interface { Print() } type Data struct { value int } func (d Data) Print() { fmt.Println("Value:", d.value) } func (d *Data) PrintPtr() { fmt.Println("Pointer Value:", d.value) } func main() { d := Data{value: 10} var p Printer = d p.Print() var p2 Printer = &d p2.Print() }
Check which methods satisfy the interface for value and pointer types.
The interface Printer requires a Print() method. The value type Data has a Print() method, so both d and &d implement Printer. The method PrintPtr() is not part of the interface, so it is not called. Therefore, both calls print "Value: 10".
Which statement about method sets and interface implementation in Go is correct?
Recall how Go defines method sets for values and pointers.
In Go, the method set of a value type T includes all methods with value receivers. The method set of *T (pointer to T) includes all methods with pointer or value receivers. Therefore, a pointer to T implements an interface if the interface methods are implemented with either pointer or value receivers. Option A is correct.
Option A is wrong because values do not have pointer receiver methods in their method set. Option A is wrong because pointer types have all methods, not just value receivers. Option A is wrong for the same reason as A.