Skip to content

Instantly share code, notes, and snippets.

@dacr
Created March 28, 2025 09:01
Show Gist options
  • Save dacr/618512d5ab9bc09243922913250754d7 to your computer and use it in GitHub Desktop.
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/c6c43932cad586b4c08aae8b849117ce4e6da489
/*?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 NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// 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