Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:25
Show Gist options
  • Select an option

  • Save dacr/618512d5ab9bc09243922913250754d7 to your computer and use it in GitHub Desktop.

Select an option

Save dacr/618512d5ab9bc09243922913250754d7 to your computer and use it in GitHub Desktop.
go composition / published by https://github.com/dacr/code-examples-manager #e191920d-73ab-4540-825e-2ecc4ca83488/db5498e798c5f80a6499b93d25ad3ab772ca7ef8
/*?sr/bin/true; exec /usr/bin/env nix-shell -p go --run "go run $0" #*/
// summary : go composition
// keywords : go, composition, @testable
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : e191920d-73ab-4540-825e-2ecc4ca83488
// created-on : 2025-03-27T15:11:42+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : nix-shell -p go --run "go run $file"
package main
type Animal struct {
name string
}
type Cat struct {
Animal
meow string
}
type Dog struct {
Animal
bark string
}
func main() {
catty := Cat{Animal{"Fluffy"}, "meow"}
doggy := Dog{Animal{"Fido"}, "woof"}
println(catty.meow, catty.Animal.name)
//animals := []Animal{catty, doggy} // USE INTERFACE INSTEAD !!!
animals := []Animal{catty.Animal, doggy.Animal}
for _, animal := range animals {
println(animal.name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment