Code-Memo

Variadic arguments and spreading

Variadic parameters

A variadic parameter must be last; it collects arguments into a slice inside the function.

func sum(nums ...int) int {
	total := 0
	for _, n := range nums {
		total += n
	}
	return total
}

sum(1, 2, 3)
Passing a slice into a variadic

Use ... after the slice to expand it.

xs := []int{1, 2, 3}
sum(xs...)

The slice is passed without copying underlying elements (same backing array semantics as usual slices).

No kwargs

Go has no keyword arguments. Use structs, functional options, or named parameters via a config struct.

type Params struct{ Timeout time.Duration; Retries int }

func Do(p Params) error { return nil }
Variadic and interfaces

fmt.Println accepts ...any (formerly ...interface{}). Each value is passed as any with dynamic type preserved.