func NewWithValue(parent Context, key, val interface{}) Context
func NewBackground() Context
you wouldn't even have to guess.I'd have expected `SetValue` for updating keys in-place.
That `WithValue` creates an "inheritance" relationship surprised me a lot more, that I did not expect.
Looking at just a function definition, you can't know if a variable is a value or a pointer unless you first determine if it's an interface type, and if it is an interface type, you have to know the concrete type of the caller to know.
However, even a struct passed by value can be mutated since interior mutability can be managed with internal pointers. For example:
type value struct {
interior *int
}
func(v value) mutate(to int) { *v.interior = to }
func(v value) String() string { return fmt.Sprintf("%d", *v.interior) }
func newValue(i int) value { return value{&i} }
func mutateValue(v value, to int) {
v.mutate(to)
}
func main() {
val := newValue(10)
mutateValue(val, 20)
fmt.Println(val)
}But if the interface is implemented by a pointer-to-T and you pass a T, you will get an error; and if you have wrapped a T in one interface and try to convert it to another interface that is only satisfied by a pointer-to-T, the conversion will fail.
So the answer to "are interfaces are always pointers?" is yes if you're asking about implementing the Go compiler or worried about the size of some allocation; but no if you are asking about Go's type system.