Skip to content

Instantly share code, notes, and snippets.

@mjs
Created March 17, 2016 20:30
Show Gist options
  • Save mjs/0819ddeb4f5ea93f0889 to your computer and use it in GitHub Desktop.
Save mjs/0819ddeb4f5ea93f0889 to your computer and use it in GitHub Desktop.
Demo of go-yaml issue under Go 1.6
package main
import (
"fmt"
// Swap these 2 for comparison. Under Go 1.6 the program works
// with json but not yaml.
"gopkg.in/yaml.v2"
// yaml "encoding/json"
)
type bar struct {
N int
}
type Foo struct {
S string
bar
}
func (f *Foo) Dump() {
fmt.Printf("S: %s; N=%d\n", f.S, f.N)
}
func main() {
x := Foo{S: "qux"}
x.N = 99
x.Dump()
bytes, err := yaml.Marshal(x)
if err != nil {
panic(err)
}
fmt.Printf("\nmarshalled:\n%s\n\n", string(bytes))
var y Foo
err = yaml.Unmarshal(bytes, &y)
if err != nil {
panic(err)
}
y.Dump()
}
@mjs
Copy link
Author

mjs commented Mar 17, 2016

Under Go 1.6 go-yaml fails like this:

panic: reflect.Value.Interface: cannot return value obtained from unexported field or method [recovered]
    panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

goroutine 1 [running]:
panic(0x536760, 0xc82000a6a0)
    /home/menno/go/golang/go1.6/src/runtime/panic.go:464 +0x3e6
gopkg.in/yaml%2ev2.handleErr(0xc82003de88)
    /home/menno/go/src/gopkg.in/yaml.v2/yaml.go:153 +0xdf
panic(0x536760, 0xc82000a6a0)
    /home/menno/go/golang/go1.6/src/runtime/panic.go:426 +0x4e9
reflect.valueInterface(0x564360, 0xc82000e570, 0xd9, 0x536701, 0x0, 0x0)
    /home/menno/go/golang/go1.6/src/reflect/value.go:919 +0xe7
reflect.Value.Interface(0x564360, 0xc82000e570, 0xd9, 0x0, 0x0)
    /home/menno/go/golang/go1.6/src/reflect/value.go:908 +0x48
gopkg.in/yaml%2ev2.(*encoder).marshal(0xc8200882c0, 0x0, 0x0, 0x564360, 0xc82000e570, 0xd9)
    /home/menno/go/src/gopkg.in/yaml.v2/encode.go:67 +0x89
gopkg.in/yaml%2ev2.(*encoder).structv.func1()
    /home/menno/go/src/gopkg.in/yaml.v2/encode.go:166 +0x81d
gopkg.in/yaml%2ev2.(*encoder).mappingv(0xc8200882c0, 0x0, 0x0, 0xc82003dc68)
    /home/menno/go/src/gopkg.in/yaml.v2/encode.go:196 +0x181
gopkg.in/yaml%2ev2.(*encoder).structv(0xc8200882c0, 0x0, 0x0, 0x568580, 0xc82000e560, 0x99)
    /home/menno/go/src/gopkg.in/yaml.v2/encode.go:184 +0x11b
gopkg.in/yaml%2ev2.(*encoder).marshal(0xc8200882c0, 0x0, 0x0, 0x568580, 0xc82000e560, 0x99)
    /home/menno/go/src/gopkg.in/yaml.v2/encode.go:101 +0xa16
gopkg.in/yaml%2ev2.Marshal(0x568580, 0xc82000e560, 0x0, 0x0, 0x0, 0x0, 0x0)
    /home/menno/go/src/gopkg.in/yaml.v2/yaml.go:142 +0x11e
main.main()
    /home/menno/go/src/freshfoo.com/embedded-struct3/main.go:29 +0xe1

encoding/json is fine.

@efairon
Copy link

efairon commented Mar 27, 2016

I'm getting the same problem. Used to work with go 1.5

I now use https://github.com/ghodss/yaml .. No more problem.. No change in my code.

panic: reflect.Value.Interface: cannot return value obtained from unexported field or method [recovered]
        panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

goroutine 20 [running]:
panic(0x807300, 0xc820223610)
        /usr/local/go/src/runtime/panic.go:464 +0x3e6
gopkg.in/yaml%2ev2.handleErr(0xc820045f00)
        /home/eric/Dropbox/go/src/gopkg.in/yaml.v2/yaml.go:153 +0xdf
panic(0x807300, 0xc820223610)
        /usr/local/go/src/runtime/panic.go:426 +0x4e9
reflect.valueInterface(0x903860, 0xc820226040, 0x1d9, 0x807301, 0x0, 0x0)
        /usr/local/go/src/reflect/value.go:919 +0xe7
reflect.Value.Interface(0x903860, 0xc820226040, 0x1d9, 0x0, 0x0)
        /usr/local/go/src/reflect/value.go:908 +0x48
gopkg.in/yaml%2ev2.(*encoder).marshal(0xc8201782c0, 0x0, 0x0, 0x903860, 0xc820226040, 0x1d9)
        /home/eric/Dropbox/go/src/gopkg.in/yaml.v2/encode.go:67 +0x89
gopkg.in/yaml%2ev2.(*encoder).structv.func1()
        /home/eric/Dropbox/go/src/gopkg.in/yaml.v2/encode.go:166 +0x81d
gopkg.in/yaml%2ev2.(*encoder).mappingv(0xc8201782c0, 0x0, 0x0, 0xc8200458c0)
        /home/eric/Dropbox/go/src/gopkg.in/yaml.v2/encode.go:196 +0x181
gopkg.in/yaml%2ev2.(*encoder).structv(0xc8201782c0, 0x0, 0x0, 0x955840, 0xc820226000, 0x199)
        /home/eric/Dropbox/go/src/gopkg.in/yaml.v2/encode.go:184 +0x11b
gopkg.in/yaml%2ev2.(*encoder).marshal(0xc8201782c0, 0x0, 0x0, 0x955840, 0xc820226000, 0x199)
        /home/eric/Dropbox/go/src/gopkg.in/yaml.v2/encode.go:101 +0xa16
gopkg.in/yaml%2ev2.(*encoder).slicev(0xc8201782c0, 0x0, 0x0, 0x916c40, 0xc82010ed80, 0x197)
        /home/eric/Dropbox/go/src/gopkg.in/yaml.v2/encode.go:212 +0x247
gopkg.in/yaml%2ev2.(*encoder).marshal(0xc8201782c0, 0x0, 0x0, 0x916c40, 0xc82010ed80, 0x197)
        /home/eric/Dropbox/go/src/gopkg.in/yaml.v2/encode.go:106 +0x988
gopkg.in/yaml%2ev2.(*encoder).marshal(0xc8201782c0, 0x0, 0x0, 0x91e840, 0xc82010ed80, 0x16)
        /home/eric/Dropbox/go/src/gopkg.in/yaml.v2/encode.go:98 +0x827
gopkg.in/yaml%2ev2.Marshal(0x91e840, 0xc82010ed80, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/eric/Dropbox/go/src/gopkg.in/yaml.v2/yaml.go:142 +0x11e
nearby/rest/lib.Users.Export.func2(0xc820223320, 0xc82010ed80)
        /home/eric/Dropbox/go/src/nearby/rest/lib/user.go:153 +0x74
created by nearby/rest/lib.Users.Export
        /home/eric/Dropbox/go/src/nearby/rest/lib/user.go:158 +0x45a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment