FlashKeepers

Go · College · College and University

Go II: Structs, Methods and Interfaces

Go structs, methods, pointer receivers, interfaces, embedding, type assertions and generics.

About this set

40 basic cards for College Go. It opens with "What is a struct in Go?" and "What syntax defines a struct in Go?". Study it alongside the other College sets below.

40 cards · basic cards · AI-written, checked twice. Edit anything.

Study this set free Look inside first Get FlashKeepers for iPhone

Opens in your browser, no account. Get the iPhone and iPad app · Android app

What is a struct in Go?
Show answerA composite data type that groups variables of different types together
What syntax defines a struct in Go?
Show answertype StructName struct { field Type; field2 Type2 }
How do you create a struct instance with values?
Show answerUse struct literal: StructName{field: value, field2: value2}
What is a method in Go?
Show answerA function with a receiver argument between func keyword and method name
What is a receiver in Go?
Show answerThe 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 answerYes, on any named type, but not on pointers or interface types
What is a pointer receiver?
Show answerA 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 answerWhen 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 answerValue 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 answerYes, Go allows both value and pointer receivers on the same type
What is an interface in Go?
Show answerA type that specifies a set of method signatures without implementation
What syntax defines an interface in Go?
Show answertype InterfaceName interface { Method1() ReturnType; Method2() ReturnType }
How does Go implement interfaces?
Show answerImplicitly: a type automatically implements an interface if it has all required methods
What is the empty interface in Go?
Show answerinterface{}, which has no methods and can hold a value of any type
What is a type assertion in Go?
Show answerThe syntax value.(Type) that extracts a concrete value from an interface

25 more cards in the app

Study this set free