Skip to content

Instantly share code, notes, and snippets.

@harlow harlow/get_set.go
Created Aug 4, 2014

What would you like to do?
Get and set values of a Struct using reflection in Go Lang
type MyStruct struct {
N int
n := MyStruct{ 1 }
// get
immutable := reflect.ValueOf(n)
val := immutable.FieldByName("N").Int()
fmt.Printf("N=%d\n", val) // prints 1
// set
mutable := reflect.ValueOf(&n).Elem()
fmt.Printf("N=%d\n", n.N) // prints 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.