Interfaces, Methods & Embedding·Q77·Hard
An interface value is a two-word structure. The runtime uses two layouts: eface (empty interface, any ): {_type *_type; data unsafe.Pointer} . It holds only the dynamic…
Interfaces, Methods & Embedding·Q78·Hard
An itab pairs one (interface type, concrete type) combination with the method pointers the concrete type provides for that interface. When the compiler can see a…
Interfaces, Methods & Embedding·Q79·Medium
package main import "fmt" type T struct{} func main() { var p *T = nil var i any = p var j any fmt.Println(p == nil) // ? fmt.Println(i == nil) // ? fmt.Println(j ==…
Interfaces, Methods & Embedding·Q80·Hard
type MyErr struct{ Msg string } func (e *MyErr) Error() string { return e.Msg } func validate(ok bool) error { var e *MyErr if !ok { e = &MyErr{"invalid"} } return e //…
Interfaces, Methods & Embedding·Q81·Medium
The spec defines method sets as follows: The method set of T contains methods declared with receiver T . The method set of *T contains methods with receiver T and *T .…
Interfaces, Methods & Embedding·Q82·Hard
type Counter struct{ n int } func (c *Counter) Inc() { c.n++ } m := map[string]Counter{"k": {}} // m["k"].Inc() // error: cannot call pointer method Inc on Counter //…
Interfaces, Methods & Embedding·Q83·Medium
Use a pointer receiver when any of these apply: The method mutates the receiver. The struct is large, so copying it on every call is expensive. The struct contains…
Interfaces, Methods & Embedding·Q84·Medium
The single-value form panics when the assertion fails. The panic message looks like interface conversion: interface {} is string, not int . The comma-ok form never…
Interfaces, Methods & Embedding·Q85·Medium
func describe(x any) string { switch v := x.(type) { case nil: return "nil" case int, int64: return fmt.Sprintf("integer %T", v) // v has type any here case error:…
Interfaces, Methods & Embedding·Q86·Medium
No. Embedding is composition with automatic delegation . An embedded field is an ordinary field named after its type, and the compiler promotes its fields and methods so…
Interfaces, Methods & Embedding·Q87·Hard
type Base struct{} func (Base) Name() string { return "Base" } func (b Base) Greet() string { return "Hello from " + b.Name() } type Derived struct{ Base } func…
Interfaces, Methods & Embedding·Q88·Hard
For a struct S : If S embeds T , the method sets of S and *S include the promoted methods with receiver T . Only *S also includes those with receiver *T . If S embeds *T…
Interfaces, Methods & Embedding·Q89·Hard
Selector resolution uses depth . The name at the shallowest depth wins and shadows any deeper ones. If two or more names exist at the same shallowest depth, the selector…
Interfaces, Methods & Embedding·Q90·Medium
An interface can embed other interfaces. The resulting method set is the union of all their methods. This is how the standard library builds larger contracts from small…
Interfaces, Methods & Embedding·Q91·Hard
Embedding an interface in a struct makes the struct satisfy the interface automatically. You then override only the methods you care about, and all the others delegate…
Interfaces, Methods & Embedding·Q92·Medium
any is an alias for interface{} (Go 1.18). Every type satisfies it, which is why it throws away static type information. Legitimate uses: Truly heterogeneous data:…
Interfaces, Methods & Embedding·Q93·Medium
It is a compile-time assertion that *MyWriter implements io.Writer . Go interfaces are satisfied implicitly, so if someone changes a method signature, the type silently…
Interfaces, Methods & Embedding·Q94·Medium
Because satisfaction is structural and there is no implements keyword, a type can satisfy an interface that was written after it, in a package it has never heard of. The…
Interfaces, Methods & Embedding·Q95·Medium
Accept interfaces: parameters should ask for the smallest behaviour needed ( io.Reader rather than *os.File ). Callers can then pass files, buffers, network connections…
Interfaces, Methods & Embedding·Q96·Medium
"The bigger the interface, the weaker the abstraction" (Rob Pike). A one-method interface is easy to satisfy, so many types implement it, and it composes well. io.Reader…
Interfaces, Methods & Embedding·Q97·Hard
type Celsius float64 func (c Celsius) String() string { return fmt.Sprintf("%v C", c) // infinite recursion -> stack overflow } %v on a value that implements…
Interfaces, Methods & Embedding·Q98·Hard
error is a one-method interface. Go offers three styles, each for a different need: Sentinel errors ( var ErrNotFound = errors.New(...) ), compared by identity. Typed…
Interfaces, Methods & Embedding·Q99·Hard
Two interface values are equal when their dynamic types are identical and their dynamic values are equal, or when both are nil. If the dynamic types are the same and…
Interfaces, Methods & Embedding·Q100·Hard
Often it does. The data word must be a pointer, so non-pointer values are copied into memory. The runtime helpers ( convT , convT64 , convTstring ) allocate on the heap…
Interfaces, Methods & Embedding·Q101·Hard
type T struct{ n int } func (t T) ShowVal() { fmt.Println("val", t.n) } func (t *T) ShowPtr() { fmt.Println("ptr", t.n) } func main() { t := T{n: 1} fv := t.ShowVal //…
Interfaces, Methods & Embedding·Q102·Medium
You can declare a method only on a defined type declared in the same package . The receiver's base type must not be a pointer type or an interface type. The base type…
Interfaces, Methods & Embedding·Q103·Medium
A function accepts a small interface, then uses a type assertion to check at runtime whether the value also implements a richer interface. If it does, the function takes…
Interfaces, Methods & Embedding·Q104·Hard
Since Go 1.18, an interface defines a type set . A basic interface (methods only) can be used both as a value type and as a constraint. An interface that contains type…
Interfaces, Methods & Embedding·Q105·Medium
These are three different mechanisms: Defined type type B A : B has the same underlying type as A but none of A's declared methods . (If A is an interface type, B has…
Interfaces, Methods & Embedding·Q106·Medium
A function type can have methods, so a plain function can be adapted to satisfy a single-method interface without writing a struct: // net/http type Handler interface {…
Interfaces, Methods & Embedding·Q107·Hard
type V struct{ n int } func (v V) Get() int { return 42 } // value receiver, never reads v.n type Getter interface{ Get() int } //go:noinline func call(g Getter) int {…
Interfaces, Methods & Embedding·Q108·Hard
type P struct{ X int } func main() { var i any = P{1} // i.(P).X = 2 // compile error: cannot assign to i.(P).X // // (neither addressable nor a map index expression) p…
Interfaces, Methods & Embedding·Q109·Hard
type Setter interface{ Set(string) } type Settable struct{ v string } func (s *Settable) Set(v string) { s.v = v } // Attempt 1: T Setter func Bad[T Setter](xs []string)…
Interfaces, Methods & Embedding·Q110·Hard
Add an unexported method to the interface. Method names that are not exported are qualified by their package, so a type in another package cannot declare a method with…
Interfaces, Methods & Embedding·Q111·Hard
// Go 1.22+: reflect.TypeFor works for interface types directly writerT := reflect.TypeFor[io.Writer]() fmt.Println(reflect.TypeFor[*bytes.Buffer]().Implements(writerT))…