Advertisement
konalisp

Go Class Testing

Aug 12th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.71 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. type Person struct {
  6.     Name string
  7. }
  8.  
  9. type Android struct {
  10.     Person //sort of like inheritence, if an "Android" is a "Person", it can use Talk().
  11.     Model string
  12. }
  13.  
  14. func (p *Person) Talk() { //"(p *Person)" means this is a method, it only works on "Person" structs, as in, structs with a "Person" type. Example: p.Talk().
  15.     fmt.Printf("Hi, my name is %s!", p.Name)
  16. }
  17.  
  18. func main() {
  19.     a := new(Android)
  20.    
  21.     defer func(s string){ //anonymous function. Will not work if "defer" is removed, as a.Name is given a value only after it's declared.
  22.         a.Talk() //instead of saying "a.Person.Talk()", we just use a.Talk(), because Android is a Person.
  23.     }(a.Name)
  24.    
  25.     a.Name = "Lil' John"
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement