Skip to content

Instantly share code, notes, and snippets.

@zhum
Created March 3, 2015 13:04
Show Gist options
  • Save zhum/7afa16fcbbf305588d57 to your computer and use it in GitHub Desktop.
Save zhum/7afa16fcbbf305588d57 to your computer and use it in GitHub Desktop.
interfaces and references example
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