I want to modify an element's value in a double-linked list, but I don't know how to get its pointer, because element's value is a nil interface defined by go-lang itself. As far as I know is, I must do a type assertion before get element's value like:
val, ok := ele.Value.(TYPE)
if ok {
// do something...
}
but if I just modify val
it will be useless. So any hint?
Thanks.
There are two pretty straight forward options. They're all going to involve type asserting because you're using interface{}
You can store it as a pointer and type assert:
var q interface{}
var i int
q = &i
*(q.(*int)) = 5
You can simply reassign it:
var q interface{}
q = 5
b := q.(int)
q = 2*b
I personally think reassigning it makes the most sense. If you're doing it in a function you probably need to return the new value. I'm sure there are other ways to change it around, but I think simple is best.
Of course in the real work some checking would be nice.