Go Programming
111 flashcards by MentisLibrary
Go as it is actually written: types and zero values, slices and maps, structs and interfaces, goroutines and channels, error handling, generics, modules and the testing and tooling that ship with the language.
- What does := do in Go?
- Short variable declaration: it declares a new variable and initializes it, inferring the type from the right-hand side. It only works inside a function.
- What is a zero value?
- Every declared variable starts as its type's zero value: 0, false, "", and nil for pointers, slices, maps, channels, functions and interfaces.
- var vs :=
- var works at package and function scope and may omit the initializer; := only works inside functions and always needs a value.
- How do you declare a constant?
- const name = value for an untyped constant, or const name Type = value for a typed one. Untyped constants take their type from the context they are used in.
- What is iota?
- A counter inside a const block that starts at 0 and increases by one on each line, used to build enumerations.
- Which loop constructs does Go have?
- Only for, in three shapes: the C-style three-clause loop, a condition-only loop that acts like while, and for range.
- What does range yield over a slice, a map, a string and a channel?
- Slice: index and element. Map: key and value. String: byte offset and rune. Channel: each received value.
- How does switch differ from C?
- Cases do not fall through, so no break is needed; a case may list several values, and a tagless switch takes boolean expressions.
- defer
- Schedules a function call to run when the surrounding function returns; multiple deferred calls run in last-in, first-out order.
- When are deferred arguments evaluated?
- At the moment the defer statement executes, even though the call itself runs later.
- How do multiple return values work?
- A function lists several result types in parentheses and returns them together; the caller receives them into several variables.
- What are named return values?
- Result parameters with names, which declares them as zero-valued variables in the function and lets a bare return send back their current values.
- How do you write a variadic function?
- Make the final parameter ...T; callers pass zero or more values and the function sees them as a []T.
- What is a closure in Go?
- A function literal that captures variables from the enclosing scope by reference, so it can read and modify them after that scope has returned.
- What changed about loop variables in Go 1.22?
- Each iteration of a for loop now gets its own copy of the loop variable, so closures created in different iterations capture different values.
- Are arguments passed by value or by reference?
- Always by value: the callee gets a copy. Slices, maps, channels, functions and pointers copy a small header or address, so the underlying data is shared.
- How do pointers work in Go?
- &x takes the address of x, *p dereferences the pointer p, and *T is the pointer-to-T type.
- new vs make
- new(T) allocates a zeroed T and returns a *T. make initializes a slice, map or channel and returns the value itself, ready to use.
- Arrays vs slices
- An array has a fixed length that is part of its type and is copied on assignment. A slice is a window onto an array, with a length and a capacity, and assignment shares the backing array.
- What are a slice's length and capacity?
- len is how many elements the slice currently holds; cap is how many the backing array can hold counting from the slice's first element.
- How does append work?
- It adds elements to a slice, reusing the backing array when capacity allows and otherwise allocating a larger array and copying. It returns the new slice header.
- What does s[lo:hi:max] mean?
- The full slice expression: elements lo up to but excluding hi, with the capacity capped at max - lo.
- What does copy do?
- copy(dst, src) copies min(len(dst), len(src)) elements from src into dst and returns how many it copied.
- How do maps behave?
- map[K]V is a hash table with comparable keys. Reading a missing key returns the zero value of V rather than an error.
- What is the comma ok idiom?
- The two-value map lookup v, ok := m[k] returns the value and a boolean saying whether the key exists.
- How do you remove a map entry?
- delete(m, key) removes it; deleting a key that is not there is a harmless no-op.
- Why can a slice not be a map key?
- Map keys must support ==, and slices, maps and functions are not comparable.
- How do you sort a slice?
- slices.Sort for ordered element types, or slices.SortFunc with a comparison function returning negative, zero or positive.
- How do you define and create a struct?
- type Name struct { fields } defines it; a composite literal with keyed fields creates a value.
- What is struct embedding?
- Listing a type inside a struct without a field name embeds it and promotes its fields and methods to the outer type.