Flashcards
Types, Methods, and Interfaces
Flashcards do conjunto (23)
- 1What is a concrete type in Go?A type that specifies both what and how, defined using primitive or compound type literals.
- 2How does an abstract type differ from a concrete type?It specifies what a type should do, but not how.
- 3Where can types be declared in Go?At any block level, accessible only within their scope unless exported.
- 4Where is a method defined in Go?At package block level with a receiver specification (abbreviation of type's name).
- 5What's the key difference between methods and functions in Go?Methods are tied to a type; functions can be defined in any block.
- 6When should you use a pointer receiver for a method?When modifying the receiver or handling nil instances.
- 7What's the best practice for choosing between pointer and value receivers?Use pointer receivers if any method uses one; consistency is key.
- 8What's the difference in method sets between pointer and value instances?Pointer instances include both pointer and value receiver methods; value instances only include value receiver methods.
- 9How do value receivers handle nil instances?They cause a panic.
- 10What is a method value in Go?A method assigned to a variable, acting like a closure.
- 11How does a method expression differ from a method value?It's a function created from a type, with the receiver as the first parameter.
- 12What's the main difference between functions and methods in terms of dependency?Methods depend on external data (struct fields), functions rely only on input parameters.
- 13How does Go use types for documentation?Types provide clear expectations for data and operations.
- 14What is the primary use case for iota in Go?Defining a sequence of constants that increment automatically.
- 15What is embedding used for in Go?Composition, allowing code reuse.
- 16What happens to methods and fields of an embedded type?They are promoted to the containing struct.
- 17How are interfaces declared in Go?Using the type keyword followed by an interface literal listing required methods.
- 18How do concrete types implement interfaces in Go?Implicitly, by having all required methods.
- 19Why is it recommended to accept interfaces but return structs?It prevents breaking changes when updating methods or fields.
- 20When is an interface considered nil in Go?When both its type and value fields are nil.
- 21What is the any type in Go?An alias for interface{}, representing any type.
- 22What's the difference between a type assertion and a type switch?Type assertion names the concrete type of an interface; type switch handles multiple possible types.
- 23How can function types be useful with interfaces?They can have methods, allowing them to implement interfaces (e.g., HTTP handlers).
Informações
- Descrição
- Book Learning Go (CH7: Types, Methods, and Interfaces) FlashCards sobre Types, Methods, and Interfaces, criado por Filipi Melo em 22-08-2024.
- Criado por
- Filipi Melo
- Visualizações
- 1
- Avaliação