Last active
August 29, 2015 13:57
-
-
Save tejainece/9395208 to your computer and use it in GitHub Desktop.
Golang: embedding pointer anonymous members
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 Mammal struct { | |
| Lung string | |
| } | |
| type Fish struct { | |
| Gills string | |
| } | |
| type Amphibian struct { | |
| Mammal | |
| Fish | |
| Name string | |
| } | |
| func main() { | |
| salamandar := Amphibian{} | |
| salamandar.Name = "Salamandar" | |
| fmt.Print(salamandar.Name + " ") | |
| salamandar.Lung = "Lung" | |
| fmt.Print("has " + salamandar.Lung) | |
| salamandar.Gills = "Gills" | |
| fmt.Println(" and " + salamandar.Gills) | |
| lion := salamandar.Mammal | |
| fmt.Println("Lion has a " + lion.Lung) | |
| } |
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 Mammal struct { | |
| Lung string | |
| } | |
| type Fish struct { | |
| Gills string | |
| } | |
| type Amphibian struct { | |
| Mammal | |
| Fish | |
| Name string | |
| } | |
| func main() { | |
| salamander := Amphibian{} | |
| salamander.Name = "salamander" | |
| fmt.Print(salamander.Name + " ") | |
| salamander.Lung = "Lung" | |
| fmt.Print("has " + salamander.Lung) | |
| salamander.Gills = "Gills" | |
| fmt.Println(" and " + salamander.Gills) | |
| nick := salamander.(Mammal) | |
| fmt.Println("Nick has a " + nick.Lung) | |
| } |
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 Mammal struct { | |
| Lung string | |
| } | |
| type Fish struct { | |
| Gills string | |
| } | |
| type Amphibian struct { | |
| Mammal | |
| Fish | |
| Name string | |
| } | |
| func main() { | |
| salamander := new(Amphibian) | |
| salamander.Name = "salamander" | |
| fmt.Print(salamander.Name + " ") | |
| salamander.Lung = "Lung" | |
| fmt.Print("has " + salamander.Lung) | |
| salamander.Gills = "Gills" | |
| fmt.Println(" and " + salamander.Gills) | |
| } |
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 Mammal struct { | |
| Lung string | |
| Lips string | |
| } | |
| type Fish struct { | |
| Gills string | |
| Lips string | |
| } | |
| type Amphibian struct { | |
| Mammal | |
| Fish | |
| Name string | |
| } | |
| func main() { | |
| salamander := new(Amphibian) | |
| salamander.Name = "salamander" | |
| fmt.Print(salamander.Name + " ") | |
| salamander.Lung = "Lung" | |
| fmt.Print("has " + salamander.Lung) | |
| salamander.Gills = "Gills" | |
| fmt.Println(" and " + salamander.Gills) | |
| salamander.Mammal.Lips = "Mammal lips" | |
| salamander.Fish.Lips = "Fish lips" | |
| fmt.Println(salamander.Name + " has both " + salamander.Mammal.Lips + " and " + salamander.Fish.Lips) | |
| } |
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 Mammal struct { | |
| Lung string | |
| Lips string | |
| } | |
| type Fish struct { | |
| Gills string | |
| Lips string | |
| } | |
| type Amphibian struct { | |
| Mammal | |
| Fish | |
| Name string | |
| } | |
| func main() { | |
| salamander := new(Amphibian) | |
| salamander.Name = "salamander" | |
| fmt.Print(salamander.Name + " ") | |
| salamander.Lung = "Lung" | |
| fmt.Print("has " + salamander.Lung) | |
| salamander.Gills = "Gills" | |
| fmt.Println(" and " + salamander.Gills) | |
| salamander.Lips = "lips" | |
| mt.Println(salamander.Name + " has " + salamander.Lips) | |
| } |
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 Mammal struct { | |
| Lung string | |
| } | |
| type Fish struct { | |
| Gills string | |
| } | |
| type Amphibian struct { | |
| *Mammal | |
| *Fish | |
| Name string | |
| } | |
| func NewAmphibian() *Amphibian { | |
| ret := new(Amphibian) | |
| ret.Mammal = new(Mammal) | |
| ret.Fish = new(Fish) | |
| return ret | |
| } | |
| func main() { | |
| salamander := NewAmphibian() | |
| salamander.Name = "salamander" | |
| fmt.Print(salamander.Name + " ") | |
| salamander.Lung = "Lung" | |
| fmt.Print("has " + salamander.Lung) | |
| salamander.Gills = "Gills" | |
| fmt.Println(" and " + salamander.Gills) | |
| } |
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 Mammal struct { | |
| Lung string | |
| } | |
| type Fish struct { | |
| Gills string | |
| } | |
| type Amphibian struct { | |
| *Mammal | |
| *Fish | |
| Name string | |
| } | |
| func main() { | |
| salamander := new(Amphibian) | |
| salamander.Name = "salamander" | |
| fmt.Print(salamander.Name + " ") | |
| salamander.Lung = "Lung" | |
| fmt.Print("has " + salamander.Lung) | |
| salamander.Gills = "Gills" | |
| fmt.Println("and " + salamander.Gills) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment