Skip to content

Instantly share code, notes, and snippets.

@victorsteven
Created June 24, 2019 15:57
Show Gist options
  • Select an option

  • Save victorsteven/e324415a7d8dc13ae2be45a232de603b to your computer and use it in GitHub Desktop.

Select an option

Save victorsteven/e324415a7d8dc13ae2be45a232de603b to your computer and use it in GitHub Desktop.
package main
import "fmt"
type animal struct {
name string
characteristics []string
}
//A herbivore is an animal, so it can have the animal struct as a field
type herbivore struct {
animal
eatHuman bool
}
func main() {
herb := herbivore{
animal: animal{
name: "Goat",
characteristics: []string{"Lacks sense",
"Lazy",
"Eat grass",
},
},
eatHuman: false, //maybe
}
//We use dot(.) to acces each field in the struct
fmt.Println("Animal name:", herb.animal.name)
fmt.Println("Eats human? ", herb.eatHuman)
fmt.Println("Characteristics:")
for _, v := range herb.animal.characteristics {
fmt.Printf("\t %v\n", v)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment