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())
}
import (
"fmt"
)
func main() {
for i, t := range []byte("我我"){
fmt.Printf("%d, %x\n",i,t)
}
for i, t := range string("我我"){
fmt.Printf("%d, %x\n",i,t)
}
for i, t := range []rune("我我"){
fmt.Printf("%d, %x\n",i,t)
}
}
/* Output:
0, e6
1, 88
2, 91
3, e6
4, 88
5, 91
0, 6211
3, 6211
0, 6211
1, 6211
Program exited. */