Created
June 24, 2019 15:57
-
-
Save victorsteven/e324415a7d8dc13ae2be45a232de603b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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