Created
July 2, 2016 00:00
-
-
Save casualjim/a1f7a245899aa3794da8a3f084dbc386 to your computer and use it in GitHub Desktop.
run this: https://play.golang.org/p/BWDlpVkoru
This file contains hidden or 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 Gettable interface { | |
Name() string | |
} | |
type gettable struct { | |
name string | |
} | |
func (g *gettable) Name() string { | |
return g.name | |
} | |
func (g *gettable) SetName(name string) { | |
g.name = name | |
} | |
func main(){ | |
g := &gettable{"some name"} | |
fmt.Println("original:", g.Name()) | |
// interfaces don't really have to exist anywhere except on a var | |
var settable interface{ | |
SetName(string) | |
} | |
settable = g | |
settable.SetName("a new name") | |
fmt.Println("new:", g.Name()) | |
// you can define types inside method bodies | |
type setter interface { | |
SetName(string) | |
} | |
var s setter = g | |
s.SetName("yet another name") | |
fmt.Println("new 2:", g.Name()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment