GO Structs and Methods 2 — Questions and Answers
Question 1: When should you prefer a pointer receiver over a value receiver for a Go method?
- When the method needs to mutate the struct or when the struct is large to avoid copying (Correct answer)
- Only when the struct contains a map or slice
- Whenever the method returns a value
- Pointer receivers should always be avoided for simplicity
Correct answer: When the method needs to mutate the struct or when the struct is large to avoid copying
Pointer receivers are preferred when the method modifies the struct or when copying the struct would be expensive due to its size.
Question 2: What happens when you assign one struct variable to another in Go?
- A shallow copy is made; the new variable is independent of the original (Correct answer)
- Both variables point to the same underlying memory
- A deep copy is performed, including all nested pointers
- A compilation error occurs because structs are reference types
Correct answer: A shallow copy is made; the new variable is independent of the original
Structs in Go are value types, so assignment creates a shallow copy where the new variable has its own memory independent of the original.
Question 3: Which of the following correctly creates a pointer to a newly allocated struct in Go?
- p := &Person{Name: "Bob"} (Correct answer)
- p := new Person{Name: "Bob"}
- p := pointer(Person{Name: "Bob"})
- p := *Person{Name: "Bob"}
Correct answer: p := &Person{Name: "Bob"}
Using `&` before a struct literal allocates the struct on the heap and returns a pointer to it.
Question 4: What does struct embedding achieve that explicit field access does not?
- Promoted methods and fields can be accessed directly without qualifying them with the embedded type's name (Correct answer)
- Embedded structs are automatically serialized to JSON with nested keys
- Only embedded interfaces are promoted; embedded struct methods are not promoted
- Embedding copies all fields into the outer struct at compile time, removing the inner type
Correct answer: Promoted methods and fields can be accessed directly without qualifying them with the embedded type's name
Struct embedding promotes the embedded type's fields and methods to the outer struct so they can be accessed without using the embedded type name as a qualifier.
Question 5: Can two structs in Go be compared with `==`? Under what condition?
- Yes, if all their fields are comparable types (no maps, slices, or functions) (Correct answer)
- No, structs in Go can never be compared directly
- Yes, always, regardless of field types
- Only if the structs implement the `Comparable` interface
Correct answer: Yes, if all their fields are comparable types (no maps, slices, or functions)
Go allows struct comparison with `==` only when all fields are of comparable types; structs containing maps, slices, or functions are not comparable.
Question 6: What is the output of the following Go code? ```go type Counter struct{ count int } func (c *Counter) Inc() { c.count++ } ct := Counter{} ct.Inc() ct.Inc() fmt.Println(ct.count) ```
- 2 (Correct answer)
- 0
- 1
- Compilation error
Correct answer: 2
The pointer receiver `*Counter` modifies the original struct, so two calls to `Inc()` increment `count` to 2.
Question 7: In Go, what is a struct tag and where is it commonly used?
- A string literal in backticks after a field type, commonly used by encoding/json to control serialization (Correct answer)
- A compiler directive placed before a struct definition to optimize layout
- A comment format used exclusively by the go vet tool
- A runtime annotation that adds methods to a struct automatically
Correct answer: A string literal in backticks after a field type, commonly used by encoding/json to control serialization
Struct tags are raw string literals (backtick strings) after the field type, and packages like `encoding/json` use them to control how fields are marshaled/unmarshaled.
When should you prefer a pointer receiver over a value receiver for a Go method?