Created
January 5, 2015 17:47
-
-
Save kevinlebrun/f9d1d4093d9b82b31d5c to your computer and use it in GitHub Desktop.
Method Expression with Go
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" | |
"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)) | |
} |
Author
kevinlebrun
commented
Jan 5, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment