Skip to content

Instantly share code, notes, and snippets.

@hogedigo
Created September 27, 2014 14:35
Show Gist options
  • Save hogedigo/6fccfa810d3609ece9f5 to your computer and use it in GitHub Desktop.
Save hogedigo/6fccfa810d3609ece9f5 to your computer and use it in GitHub Desktop.
reflect unexported field
package data
type s struct {
a, b string
}
func NewS(a, b string) s {
return s{a, b}
}
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