Created
September 27, 2014 14:35
-
-
Save hogedigo/6fccfa810d3609ece9f5 to your computer and use it in GitHub Desktop.
reflect unexported field
This file contains 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 data | |
type s struct { | |
a, b string | |
} | |
func NewS(a, b string) s { | |
return s{a, b} | |
} |
This file contains 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" | |
"reflecttest/data" | |
) | |
func main() { | |
s := data.NewS("aaa", "bbb") | |
v := reflect.ValueOf(s) | |
fmt.Println(v.FieldByName("a")) | |
fmt.Println(v.FieldByName("b")) | |
t := reflect.TypeOf(s) | |
f, ok := t.FieldByName("a") | |
fmt.Println(f, ok) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment