← ClaudeAtlas

go-interfaceslisted

Go interfaces, type assertions, type switches, and embedding from Effective Go. Covers implicit interface satisfaction, comma-ok idiom, generality through interface returns, interface and struct embedding for composition. Use when defining or implementing interfaces, using type assertions/switches, or composing types through embedding.
dwana1/golang-skills · ★ 0 · AI & Automation · score 72
Install: claude install-skill dwana1/golang-skills
# Go Interfaces and Composition Go's interfaces enable flexible, decoupled designs through implicit satisfaction and composition. This skill covers interface fundamentals, type inspection, and Go's approach to composition over inheritance. > **Source**: [Effective Go](https://go.dev/doc/effective_go) --- ## Interface Basics Interfaces in Go specify behavior: if something can do *this*, it can be used *here*. Types implement interfaces implicitly—no `implements` keyword needed. ```go // io.Writer interface - any type with this method satisfies it type Writer interface { Write(p []byte) (n int, err error) } ``` A type satisfies an interface by implementing its methods: ```go type ByteSlice []byte // ByteSlice now implements io.Writer func (p *ByteSlice) Write(data []byte) (n int, err error) { *p = append(*p, data...) return len(data), nil } // Can be used anywhere io.Writer is expected var w io.Writer = &ByteSlice{} fmt.Fprintf(w, "Hello, %s", "World") ``` ### Multiple Interface Implementation A type can implement multiple interfaces simultaneously: ```go type Sequence []int // Implements sort.Interface func (s Sequence) Len() int { return len(s) } func (s Sequence) Less(i, j int) bool { return s[i] < s[j] } func (s Sequence) Swap(i, j int) { s[i], s[j] = s[j], s[i] } // Implements fmt.Stringer func (s Sequence) String() string { sort.Sort(s) return fmt.Sprint([]int(s)) } ``` ### Interface Naming By convention, one-method inte