Skip to content

Instantly share code, notes, and snippets.

@kevinlebrun
Created January 5, 2015 17:47
Show Gist options
  • Save kevinlebrun/f9d1d4093d9b82b31d5c to your computer and use it in GitHub Desktop.
Save kevinlebrun/f9d1d4093d9b82b31d5c to your computer and use it in GitHub Desktop.
Method Expression with Go
package main
import (
"fmt"
"reflect"
)
type T struct {
Message string
}
func (t T) String() string {
return fmt.Sprintf("message: %v", t.Message)
}
func (t *T) SetMessage(message string) {
t.Message = message
}
func main() {
t := &T{"Hello World!"}
fmt.Println(reflect.TypeOf(t.String))
fmt.Println(t)
f := T.String
fmt.Println(reflect.TypeOf(f))
fmt.Println(f(*t))
fmt.Println(T.String(*t))
(*T).SetMessage(t, "My New Hello World!")
fmt.Println(T.String(*t))
}
@kevinlebrun
Copy link
Author

go run method.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment