Krut
Krut

Reputation: 4220

setting a value of a pointer does not work through interface{}

Below is slightly modified example from the go laws of reflection http://blog.golang.org/laws-of-reflection. The 2nd code section uses a pointer from a map[string]interface{} and it does not work, what am I doing wrong?

Thanks

//http://play.golang.org/p/LuMBUWLVT6
package main

import (
  "fmt"
  "reflect"
)

type T struct {
  x float64
}

func (x T) RowMap() map[string]interface{} {
  return map[string]interface{}{
    "x": &x.x,
  }
}

func main() {

  // this section works as expected, x.x will be 7.1 when done
  var x = T{3.4}
  p := reflect.ValueOf(&x.x) // Note: take the address of x.
  v := p.Elem()
  v.SetFloat(7.1)
  fmt.Println(x.x, x) // 7.1 {7.1}

  // this section I do not understand why x.x is not being set to 7.1
  x = T{3.4}
  rowmap := x.RowMap()
  p = reflect.ValueOf(rowmap["x"]) // rowmap["x"] => &x.x just like above, but is containted in interface{}
  v = p.Elem()
  v.SetFloat(7.1)
  fmt.Println(x.x, x) // 3.4 {3.4}  ?? huh, should be // 7.1 {7.1}

}

Upvotes: 0

Views: 83

Answers (1)

Jeff
Jeff

Reputation: 7210

Elem returns the value that the interface v contains or that the pointer v points to.

Try printing the following and you'll see what you want to see but x does not change meaning it's never being updated.

fmt.Println(v.Float()) // 7.1

You need to pass a pointer to your method. Change your method signature to look like this

func (x *T) RowMap() map[string]interface{} {

Pass a pointer instead of a copy.

I've added some print statements that I think will help clear things up http://play.golang.org/p/xcFMicIPcP

Look at the address of x inside and outside of your method and see how they're different.

Upvotes: 2

Related Questions