Created
October 22, 2014 07:07
-
-
Save squiidz/7b28613955ab240d147b to your computer and use it in GitHub Desktop.
Learn Go
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 Space interface { | |
Travel(int) int | |
Destroy() | |
GetName() string | |
GetSize() int | |
} | |
type Planet struct { | |
Name string | |
Size int | |
Type string | |
Life bool | |
} | |
func (p *Planet) GetName() string { | |
return p.Name | |
} | |
func (p *Planet) GetSize() int { | |
return p.Size | |
} | |
func (p *Planet) Destroy() { | |
fmt.Println("[-]", p.Name, "is destroyed") | |
if p.Life { | |
fmt.Println("[!] And everyone died !!") | |
} | |
} | |
func (p *Planet) Travel(m int) int { | |
travel := p.Size - m | |
return travel | |
} | |
func NewPlanet(name string, size int, t string, life bool) *Planet { | |
return &Planet{ | |
Name: name, | |
Size: size, | |
Type: t, | |
Life: life, | |
} | |
} | |
func Test(sp ...Space) { | |
for _, s := range sp { | |
fmt.Println("##### ", s.GetName(), " #####") | |
fmt.Println("[*] Size:", s.GetSize()) | |
s.Destroy() | |
} | |
} | |
func main() { | |
mars := &Planet{ | |
Name: "Mars", | |
Size: 1500, | |
Type: "Solid", | |
Life: false, | |
} | |
terre := &Planet{ | |
Name: "Terre", | |
Size: 34000, | |
Type: "Solid", | |
Life: true, | |
} | |
venus := NewPlanet("venus", 2500000, "Gaz", false) | |
Test(mars, terre, venus) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment