Created
March 3, 2015 13:04
-
-
Save zhum/7afa16fcbbf305588d57 to your computer and use it in GitHub Desktop.
interfaces and references example
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 "log" | |
type A interface { | |
SetId(string) | |
} | |
type B struct{ | |
Afield A | |
} | |
func (b B)SetA(a A) { | |
b.Afield=a | |
} | |
// A interface implementation | |
type Atype struct{ | |
id string | |
} | |
func (a Atype) SetId(s string){ | |
a.id=s | |
} | |
// constructor for Atype | |
func NewA() *Atype{ | |
ret:=new(Atype) | |
ret.SetId("qwe") | |
return ret | |
} | |
func main(){ | |
b:=new(B) | |
a:=NewA() | |
log.Printf("NewA: %+v",a) | |
a.SetId("asd") | |
log.Printf("Update A: %+v",a) | |
b.SetA(a) | |
log.Printf("B: %+v",b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment