Skip to content

Instantly share code, notes, and snippets.

@tejainece
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save tejainece/9395208 to your computer and use it in GitHub Desktop.

Select an option

Save tejainece/9395208 to your computer and use it in GitHub Desktop.
Golang: embedding pointer anonymous members
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
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