Description
Came across a problem with decoding pointers.
Example of code to reproduce common issue:
package main
|
|
|
import (
|
"github.com/mongodb/mongo-go-driver/bson"
|
"log"
|
)
|
|
|
type TestStruct struct {
|
TestValue *string `bson:"test_value,omitempty"`
|
}
|
|
|
func main() {
|
testString := "test value goes here"
|
t := TestStruct{
|
TestValue: &testString,
|
}
|
data, err := bson.Marshal(t)
|
if err != nil {
|
log.Fatalf("Failed to marshal. Error: %v", err)
|
}
|
unt := TestStruct{}
|
err = bson.Unmarshal(data, &unt)
|
if err != nil {
|
log.Fatalf("Failed to unmarshal. Error: %v", err)
|
}
|
log.Printf("Result: %+v")
|
if unt.TestValue == nil {
|
return
|
}
|
log.Printf("Contained string: %+v", *unt.TestValue)
|
}
|
Code like the one above gives error like:
reflect.Value.Addr of unaddressable value
|