Last active
August 29, 2015 14:15
-
-
Save t9md/911edc4530d532d1805d to your computer and use it in GitHub Desktop.
This file contains 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 Human struct { | |
name string | |
age int | |
phone string | |
} | |
// with pointer dereference | |
func (h *Human) Hi() { | |
fmt.Printf("Hi, I am %s you can call me on %s\n", (*h).name, (*h).phone) | |
} | |
func (h *Human) SetName(name string) { | |
(*h).name = name | |
} | |
// without pointer dereference | |
func (h *Human) SetAge(age int) { | |
h.age = age | |
} | |
func (h *Human) GoodBye() { | |
fmt.Printf("GoodBye, I am %s you can call me on %s\n", h.name, h.phone) | |
} | |
func main() { | |
taku := Human{name: "Mike", age: 25, phone: "222-222-XXX"} | |
taku.Hi() | |
taku.GoodBye() | |
fmt.Println("--change-------") | |
taku.SetName("Taro") | |
taku.SetAge(10) | |
taku.Hi() | |
taku.GoodBye() | |
} | |
/* output | |
Hi, I am Mike you can call me on 222-222-XXX | |
GoodBye, I am Mike you can call me on 222-222-XXX | |
--change------- | |
Hi, I am Taro you can call me on 222-222-XXX | |
GoodBye, I am Taro you can call me on 222-222-XXX | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment