GO Structs and Methods 1 — Questions and Answers
Question 1: How do you define a method on a struct type `Point` in Go?
- func (p Point) Method() {} (Correct answer)
- func Point.Method() {}
- func Method(Point p) {}
- Point.func Method() {}
Correct answer: func (p Point) Method() {}
In Go, methods are defined with a receiver between the `func` keyword and the method name, written as `func (p Point) Method() {}`.
Question 2: Which statement correctly initializes a struct `Person{Name string; Age int}` with named fields?
- Person{Name: "Alice", Age: 30} (Correct answer)
- Person("Alice", 30)
- new Person{"Alice", 30}
- Person[Name="Alice", Age=30]
Correct answer: Person{Name: "Alice", Age: 30}
Go struct literals use the syntax `TypeName{Field: value}` with a colon between field name and value.
Question 3: What is the difference between a value receiver and a pointer receiver in a Go method?
- A pointer receiver can modify the original struct; a value receiver operates on a copy (Correct answer)
- A value receiver is faster in all cases; a pointer receiver is always slower
- Pointer receivers cannot be used with structs that implement interfaces
- There is no functional difference; both operate on the same data
Correct answer: A pointer receiver can modify the original struct; a value receiver operates on a copy
A pointer receiver (`*T`) allows the method to modify the underlying struct, while a value receiver (`T`) works on a copy and cannot affect the original.
Question 4: In Go, can a struct embed another struct? What is this feature called?
- Yes, it is called struct embedding and promotes the embedded fields and methods (Correct answer)
- No, Go does not support any form of struct composition
- Yes, but only if both structs are in the same package
- Yes, but embedded structs must be interfaces, not concrete types
Correct answer: Yes, it is called struct embedding and promotes the embedded fields and methods
Go supports struct embedding, where an embedded type's fields and methods are promoted to the outer struct, enabling composition-based reuse.
Question 5: What does the following code print? ```go type Rect struct{ W, H int } func (r Rect) Area() int { return r.W * r.H } fmt.Println(Rect{3, 4}.Area()) ```
- 12 (Correct answer)
- 7
- 34
- Compilation error
Correct answer: 12
The `Area()` method multiplies `W` (3) by `H` (4), returning 12, which `fmt.Println` prints.
Question 6: Which Go construct allows you to create an anonymous struct inline?
- struct{ Field Type }{value} (Correct answer)
- anonymous{ Field Type }
- inline struct(Field Type)
- var s = struct(Field string)
Correct answer: struct{ Field Type }{value}
Anonymous structs are created inline using `struct{ Field Type }{value}` syntax without giving the struct a named type.
Question 7: What keyword is used to access the zero value of a struct in Go when no initialization is provided?
- No keyword needed; uninitialized structs automatically have zero values for all fields (Correct answer)
- nil
- zero
- default
Correct answer: No keyword needed; uninitialized structs automatically have zero values for all fields
In Go, declaring a struct variable without initialization automatically sets all fields to their zero values (0 for ints, "" for strings, nil for pointers, etc.).
How do you define a method on a struct type `Point` in Go?