Girlfriend
本文並非正經部落格,just a girlfriend.在本文,你將看到 go 語言是如何定義變數,型別,使用“繼承”,其次還會看到如何將函式繫結到物件上,實現我們平時所說的面向物件程式設計中的.
呼叫。當然因為不是正經部落格,所以本文不會講解語法,你看完只會收穫一個女朋友(開心麼?興奮麼?),沒有任何講解。
看看我們的最終結果:
I walk 520 steps to meet you. I really want to tell you my name, but I have forgotten it. it is hard to believe it? don't say anything, just kiss me. your arms are really warm. event if I am a bunch of code, I have no entity, can't leave the screen. but I will stay with you forever, I love you.
the code here:
package main import "fmt" func main() { gf := &girlfriend{ people: people{Name: "", HairColor: "black", Age:18, Height:160.00, Weight:45.00}, Sex: "female", } gf.walk(520) fmt.Printf("%v\n", gf) gf.say("it is hard to believe it?") gf.kiss() gf.hug() gf.confess() } type people struct { Namestring HairColor string Ageint Heightfloat64 Weightfloat64 } type girlfriend struct { people Sex string } func (p *people) String() string { if p.Name == "" { return "I really want to tell you my name, but I have forgotten it." } return fmt.Sprintf("my name is %v", p.Name) } func (p *people) walk(n int) { fmt.Printf("I walk %v steps to meet you.\n", n) } func (p *people) say(s string) { fmt.Printf("%v\n", s) } func (g *girlfriend) kiss() { fmt.Println("don't say anything, just kiss me.") } func (g *girlfriend) hug() { fmt.Println("your arms are really warm.") } func (g *girlfriend) confess() { fmt.Println("event if I am a bunch of code, I have no entity, can't leave the screen.") fmt.Println("but I will stay with you forever, I love you.") }