Language Fundamentals & Types·Q1·Medium
Every variable in Go is initialized to its type's zero value: 0 for numbers, false for bools, "" for strings, and nil for pointers, slices, maps, channels, functions and…
Language Fundamentals & Types·Q2·Medium
new(T) works for any type. It allocates a zeroed T and returns a *T . make only works for slices, maps and channels. It initializes the runtime data structure behind…
Language Fundamentals & Types·Q3·Hard
type Size int64 const ( _ = iota // iota = 0, skipped KB Size = 1 << (10 * iota) // iota = 1 MB // iota = 2: repeats the expression GB // iota = 3 ) const ( A, B = iota,…
Language Fundamentals & Types·Q4·Hard
Untyped constants are exact values that live only at compile time. The spec requires at least 256 bits of precision for integer constants. A constant only gets a…
Language Fundamentals & Types·Q5·Medium
A typed constant follows the same strict rules as a variable of that type. Go never converts between named types implicitly. An untyped constant can be assigned to any…
Language Fundamentals & Types·Q6·Medium
var db *sql.DB func initDB() error { db, err := sql.Open("postgres", dsn) // BUG: declares a new local db if err != nil { return err } return db.Ping() } // Later:…
Language Fundamentals & Types·Q7·Hard
func parse(s string) (n int, err error) { if s != "" { n, err := strconv.Atoi(s) if err != nil { return // compile error: result parameter err not in scope at return } _…
Language Fundamentals & Types·Q8·Hard
i := 0 s := []int{10, 20} i, s[i] = 1, 5 fmt.Println(i, s) a, b := 1, 2 a, b = b, a+b fmt.Println(a, b) Output: 1 [5 20] 2 3 A tuple assignment happens in two phases .…
Language Fundamentals & Types·Q9·Medium
A conversion , T(x) , is checked at compile time: the compiler rejects it unless the types are convertible. It may still do work at run time, such as copying bytes for…
Language Fundamentals & Types·Q10·Hard
func describe(x any) string { switch v := x.(type) { case nil: return "nil interface" case int, int64: // v has type any here, NOT int return fmt.Sprintf("integer %v",…
Language Fundamentals & Types·Q11·Medium
s := "héllo" fmt.Println(len(s)) fmt.Println(s[1]) fmt.Println(utf8.RuneCountInString(s)) for i, r := range s { fmt.Printf("%d:%c ", i, r) } Output: 6 195 5 0:h 1:é 3:l…
Language Fundamentals & Types·Q12·Hard
s := "a\xffb" fmt.Println(len(s)) // 3 for i, r := range s { fmt.Printf("%d:%U ", i, r) } // 0:U+0061 1:U+FFFD 2:U+0062 fmt.Println(string(rune(65))) // "A"…
Language Fundamentals & Types·Q13·Hard
Converting []byte(s) or string(b) normally allocates and copies the data. This is required because strings are immutable and byte slices are not. The compiler skips the…
Language Fundamentals & Types·Q14·Medium
func main() { x := 1 defer fmt.Println("deferred arg:", x) defer func() { fmt.Println("closure:", x) }() x = 2 fmt.Println("body:", x) } Output: body: 2 closure: 2…
Language Fundamentals & Types·Q15·Medium
Deferred calls are pushed onto a stack for each function call and run in LIFO order when the function returns, whether it returns normally or by panicking. A defer runs…
Language Fundamentals & Types·Q16·Hard
func f() (n int) { defer func() { n *= 2 }() return 3 } func g() int { n := 3 defer func() { n *= 2 }() return n } func safeDiv(a, b int) (q int, err error) { defer…
Language Fundamentals & Types·Q17·Hard
type T struct{ name string } func (t T) ValPrint() { fmt.Println("val:", t.name) } func (t *T) PtrPrint() { fmt.Println("ptr:", t.name) } func main() { t := T{"a"} defer…
Language Fundamentals & Types·Q18·Hard
recover stops a panic only when it is called directly by a deferred function while the goroutine is panicking. In every other situation it returns nil and does nothing.…
Language Fundamentals & Types·Q19·Medium
panic: yes. Deferred calls run while the stack unwinds, and that is where recover can happen. runtime.Goexit: yes. It ends the current goroutine and runs its defers.…
Language Fundamentals & Types·Q20·Hard
package main import "fmt" var a = c + b var b = f() var c = 1 func f() int { fmt.Println("f called, c =", c) return 2 } func init() { fmt.Println("init: a =", a) } func…
Language Fundamentals & Types·Q21·Medium
Order of initialization: Imported packages are initialized first, depth-first. Each package is initialized exactly once , no matter how many packages import it. Inside a…
Language Fundamentals & Types·Q22·Medium
An unlabeled break ends the innermost for , switch or select . Inside a select or switch that sits in a loop, a plain break leaves only the select or switch , and the…
Language Fundamentals & Types·Q23·Hard
Go has goto , but with two rules that keep it safe: It can't jump over a variable declaration if the variable would be in scope at the label. That would create a…
Language Fundamentals & Types·Q24·Medium
switch x := 2; x { case 1: fmt.Println("one") case 2: fmt.Println("two") fallthrough case 3: fmt.Println("three") fallthrough case 100: fmt.Println("hundred") default:…
Language Fundamentals & Types·Q25·Hard
Comparable types ( == and != work): booleans, numbers, strings, pointers, channels, interfaces, arrays of comparable elements, and structs whose fields are all…
Language Fundamentals & Types·Q26·Hard
Two struct values are equal when every field is equal, compared in the order the fields are declared. Unexported fields are compared too. Blank ( _ ) fields are skipped.…
Language Fundamentals & Types·Q27·Hard
comparable is a built-in constraint that permits == and != on a type parameter, so it can be used as a map key: func Index[T comparable](s []T, v T) int { for i, x :=…
Language Fundamentals & Types·Q28·Hard
type Speaker interface{ Speak() string } type Dog struct{ name string } func (d *Dog) Speak() string { return d.name + ": woof" } func main() { // var s Speaker =…
Language Fundamentals & Types·Q29·Hard
An expression is addressable if you can take its address with & . That includes variables, pointer dereferences, slice elements, and fields or elements of addressable…
Language Fundamentals & Types·Q30·Hard
Embedding promotes the embedded type's fields and methods to the outer type. For a struct S : If S embeds T : the method set of S gets T 's value methods. The method set…
Language Fundamentals & Types·Q31·Hard
type MyErr struct{} func (*MyErr) Error() string { return "boom" } func do(fail bool) error { var p *MyErr // nil pointer if fail { p = &MyErr{} } return p // BUG:…
Language Fundamentals & Types·Q32·Medium
type Celsius float64 creates a new, distinct type . It has the same underlying type, but it doesn't inherit the methods of the original named type, and you need explicit…
Language Fundamentals & Types·Q33·Hard
arr := [3]int{1, 2, 3} for i, v := range arr { if i == 0 { arr[1] = 100 } fmt.Print(v, " ") } fmt.Println() sl := []int{1, 2, 3} for i, v := range sl { if i == 0 { sl[1]…
Language Fundamentals & Types·Q34·Hard
var x int8 = 127 x++ fmt.Println(x) // -128: signed overflow wraps (two's complement), no panic var u uint8 = 0 u-- fmt.Println(u) // 255 fmt.Println(-7 / 2, -7 % 2) //…
Language Fundamentals & Types·Q35·Hard
var fns []func() for i := 0; i < 3; i++ { fns = append(fns, func() { fmt.Print(i, " ") }) } for _, f := range fns { f() } var ptrs []*int for i := range 3 { // range…
Language Fundamentals & Types·Q36·Hard
func Countdown(n int) iter.Seq[int] { // iter.Seq[V] is func(yield func(V) bool) return func(yield func(int) bool) { defer fmt.Println("iterator cleanup") for i := n; i…
Language Fundamentals & Types·Q37·Hard
type Bad struct { a bool // offset 0, then 7 bytes padding b int64 // offset 8 c bool // offset 16, then 7 bytes tail padding } type Good struct { b int64 // offset 0 a…
Language Fundamentals & Types·Q38·Hard
type P struct{ X int } func (p P) Get() int { return p.X } func (p *P) Set(v int) { p.X = v } func main() { p := P{1} get := p.Get // method value: a copy of p is bound…
Language Fundamentals & Types·Q39·Hard
var s uint = 33 var i = 1 << s // 1 takes type int var j int32 = 1 << s // 1 takes type int32: the bit is shifted out, j == 0 var k = uint64(1 << s) // 1 takes type…