GoConqr has a new look! Let us know what you think.

Flashcards

Types, Methods, and Interfaces

Filipi Melo Learning Go Book English 23 cards

Cards in this deck (23)

  1. 1What is a concrete type in Go?A type that specifies both what and how, defined using primitive or compound type literals.
  2. 2How does an abstract type differ from a concrete type?It specifies what a type should do, but not how.
  3. 3Where can types be declared in Go?At any block level, accessible only within their scope unless exported.
  4. 4Where is a method defined in Go?At package block level with a receiver specification (abbreviation of type's name).
  5. 5What's the key difference between methods and functions in Go?Methods are tied to a type; functions can be defined in any block.
  6. 6When should you use a pointer receiver for a method?When modifying the receiver or handling nil instances.
  7. 7What's the best practice for choosing between pointer and value receivers?Use pointer receivers if any method uses one; consistency is key.
  8. 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.
  9. 9How do value receivers handle nil instances?They cause a panic.
  10. 10What is a method value in Go?A method assigned to a variable, acting like a closure.
  11. 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.
  12. 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.
  13. 13How does Go use types for documentation?Types provide clear expectations for data and operations.
  14. 14What is the primary use case for iota in Go?Defining a sequence of constants that increment automatically.
  15. 15What is embedding used for in Go?Composition, allowing code reuse.
  16. 16What happens to methods and fields of an embedded type?They are promoted to the containing struct.
  17. 17How are interfaces declared in Go?Using the type keyword followed by an interface literal listing required methods.
  18. 18How do concrete types implement interfaces in Go?Implicitly, by having all required methods.
  19. 19Why is it recommended to accept interfaces but return structs?It prevents breaking changes when updating methods or fields.
  20. 20When is an interface considered nil in Go?When both its type and value fields are nil.
  21. 21What is the any type in Go?An alias for interface{}, representing any type.
  22. 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.
  23. 23How can function types be useful with interfaces?They can have methods, allowing them to implement interfaces (e.g., HTTP handlers).

Information

Description
Book Learning Go (CH7: Types, Methods, and Interfaces) Flashcards on Types, Methods, and Interfaces, created by Filipi Melo on 22/08/2024.
Created by
Filipi Melo
Views
1
Rating

More on Go Programming

Feedback

What is it about?

What do you think of the new look?

How do you like it?