Yes and no. Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy. The concept of “interface” in Go provides a different approach that we believe is easy to use and in some ways more general. There are also ways to embed types in other types to provide something analogous—but not identical—to subclassing. Moreover, methods in Go are more general than in C++ or Java: they can be defined for any sort of data, even built-in types such as plain, “unboxed” integers. They are not restricted to structs (classes).
type Creature struct {
Name string
Real bool
}
func (c Creature) Dump() {
fmt.Printf("Name: '%s', Real: %t\n", c.Name, c.Real)
}
type FlyingCreature struct {
Creature
WingSpan int
}
dragon := &FlyingCreature{
Creature{"Dragon", false, },
15,
}
fmt.Println(dragon.Name)
fmt.Println(dragon.Real)
fmt.Println(dragon.WingSpan)
type Fooer interface {
Foo1()
Foo2()
Foo3()
}
type Foo struct {}
func (f Foo) Foo1() {
fmt.Println("Foo1() here")
}
func (f Foo) Foo2() {
fmt.Println("Foo2() here")
}
func (f Foo) Foo3() {
fmt.Println("Foo3() here")
}
type foo struct {}
func (f foo) Foo1() {
fmt.Println("Foo1() here")
}
func (f foo) Foo2() {
fmt.Println("Foo2() here")
}
func (f foo) Foo3() {
fmt.Println("Foo3() here")
}
func NewFoo() Fooer {
return &foo{}
}
package main
import (
"fmt"
)
type foo struct{}
type Fooer interface {
Foo1()
Foo2()
Foo3()
}
func (f foo) Foo1() {
fmt.Println("Foo1() here")
}
func (f foo) Foo2() {
fmt.Println("Foo2() here")
}
func (f foo) Foo3() {
fmt.Println("Foo3() here")
}
func NewFoo() Fooer {
return &foo{}
}
func main() {
f := NewFoo()
f.Foo1()
f.Foo2()
f.Foo3()
}
package main
type Fooer interface {
Foo1()
Foo2()
Foo3()
}
type SuperFooer struct {
Fooer
}
func main() {
s := SuperFooer{}
s.Foo2()
}
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x28 pc=0x2a78]
goroutine 1 [running]:
panic(0xde180, 0xc82000a0d0)
/usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
main.main()
/Users/gigi/Documents/dev/go/src/github.com/oop_test/main.go:104 +0x48
exit status 2
Process finished with exit code 1
package main
import "fmt"
type Creature struct {
Name string
Real bool
}
func Dump(c *Creature) {
fmt.Printf("Name: '%s', Real: %t\n", c.Name, c.Real)
}
func (c Creature) Dump() {
fmt.Printf("Name: '%s', Real: %t\n", c.Name, c.Real)
}
type FlyingCreature struct {
Creature
WingSpan int
}
func (fc FlyingCreature) Dump() {
fmt.Printf("Name: '%s', Real: %t, WingSpan: %d\n",
fc.Name,
fc.Real,
fc.WingSpan)
}
type Unicorn struct {
Creature
}
type Dragon struct {
FlyingCreature
}
type Pterodactyl struct {
FlyingCreature
}
func NewPterodactyl(wingSpan int) *Pterodactyl {
pet := &Pterodactyl{
FlyingCreature{
Creature{
"Pterodactyl",
true,
},
wingSpan,
},
}
return pet
}
type Dumper interface {
Dump()
}
type Door struct {
Thickness int
Color string
}
func (d Door) Dump() {
fmt.Printf("Door => Thickness: %d, Color: %s", d.Thickness, d.Color)
}
func main() {
creature := &Creature{
"some creature",
false,
}
uni := Unicorn{
Creature{
"Unicorn",
false,
},
}
pet1 := &Pterodactyl{
FlyingCreature{
Creature{
"Pterodactyl",
true,
},
5,
},
}
pet2 := NewPterodactyl(8)
door := &Door{3, "red"}
Dump(creature)
creature.Dump()
uni.Dump()
pet1.Dump()
pet2.Dump()
creatures := []Creature{
*creature,
uni.Creature,
pet1.Creature,
pet2.Creature}
fmt.Println("Dump() through Creature embedded type")
for _, creature := range creatures {
creature.Dump()
}
dumpers := []Dumper{creature, uni, pet1, pet2, door}
fmt.Println("Dump() through Dumper interface")
for _, dumper := range dumpers {
dumper.Dump()
}
}
Golang Tutorial Series: Learn Go: Beginners Guide to Learn Golang from Scratch!
Write your response...