Go structs, methods, pointer receivers, interfaces, embedding, type assertions and generics.
40 cards · basic cards · AI-written, checked twice. Edit anything.
- What is a struct in Go?
Show answer
A composite data type that groups variables of different types together
- What syntax defines a struct in Go?
Show answer
type StructName struct { field Type; field2 Type2 }
- How do you create a struct instance with values?
Show answer
Use struct literal: StructName{field: value, field2: value2}
- What is a method in Go?
Show answer
A function with a receiver argument between func keyword and method name
- What is a receiver in Go?
Show answer
The argument that appears between func and the method name, binding the method to a type
- Can methods be defined on non-struct types in Go?
Show answer
Yes, on any named type, but not on pointers or interface types
- What is a pointer receiver?
Show answer
A receiver that is a pointer to a type, written as *Type between func and method name
- When should you use a pointer receiver instead of a value receiver?
Show answer
When the method needs to modify the receiver, or when the receiver is large and copying is expensive
- What is the key difference between value and pointer receivers?
Show answer
Value receiver gets a copy of the value; pointer receiver works with the original
- Can you define both value and pointer receivers on the same type?
Show answer
Yes, Go allows both value and pointer receivers on the same type
- What is an interface in Go?
Show answer
A type that specifies a set of method signatures without implementation
- What syntax defines an interface in Go?
Show answer
type InterfaceName interface { Method1() ReturnType; Method2() ReturnType }
- How does Go implement interfaces?
Show answer
Implicitly: a type automatically implements an interface if it has all required methods
- What is the empty interface in Go?
Show answer
interface{}, which has no methods and can hold a value of any type
- What is a type assertion in Go?
Show answer
The syntax value.(Type) that extracts a concrete value from an interface