The result of yaml.Unmarshal() is different between osX and linux

114 views
Skip to first unread message

dev.ys...@gmail.com

unread,
Mar 16, 2018, 8:40:02 AM3/16/18
to golang-nuts
Recently I started to use golang in project and I got one problem.
The problem is the result of yaml.Unmarshal when I put invalid yaml is different between osX and linux.

My environments are here

macOS Sierra 10.12.6
go version go1.10 darwin/amd64

Linux version 3.10.0-693.11.6.el7.x86_64 
go version go1.10 linux/amd64


This is my code I ran.

invalid.yaml: 
---------------------------------------------------------
  namespace: aaaa
name: bbbbbbb
 description:  hyoooooooooooooooooooooooo
maintainer: du...@example.com
version: 1.1.1
format::
  :type: sample
  file: ./foo.sh
---------------------------------------------------------
main.go:
---------------------------------------------------------
package main import ( "fmt" "io/ioutil" "os" yaml "gopkg.in/yaml.v2" ) type Sample struct { Namespace string `yaml:"namespace"` Name string `yaml:"name"` Description string `yaml:"description"` Maintainer string `yaml:"maintainer"` Version string `yaml:"version"` Format struct { Type string `yaml:"type"` File string `yaml:"file"` } `yaml:"format"` } const ( validYaml = "./valid.yaml" invalidYaml = "./invalid.yaml" ) func main() { data2, err := ioutil.ReadFile(invalidYaml) if err != nil { fmt.Println("It will not come here 2") os.Exit(1) } sample2 := &Sample{} err = yaml.Unmarshal(data2, sample2) if err != nil { fmt.Println("Should come here") os.Exit(0) } fmt.Println("It will not come here!!!!!!") os.Exit(1) }
---------------------------------------------------------
Expect to see osX "Should come here"
linux
"Should come here"

See instead
osX
"Should come here"
linux
"It will not come here!!!!!!"

First time I thought this is a problem of library but the library looks like does not have the difference between linux and osX.
So I ask this problem here.

I hope to fix this problem.

Thank you
Yuichi

Lutz Horn

unread,
Mar 16, 2018, 8:43:29 AM3/16/18
to golan...@googlegroups.com
> package main import ( "fmt" "io/ioutil" "os" yaml "gopkg.in/yaml.v2" )
> type
[...]

This is hard to read. Please post your code with proper line breaks.

Lutz

Jakob Borg

unread,
Mar 16, 2018, 9:24:56 AM3/16/18
to dev.ys...@gmail.com, golang-nuts
You’re getting an error on the file read. Print the error to see what it is, instead of a hardcoded string.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

y.08...@gmail.com

unread,
Mar 16, 2018, 10:24:44 AM3/16/18
to golang-nuts
Sorry, I thought I can send code with proper line breaks.
I will write code again with showing error. Also, I tried to make code simpler.
 
invalid.yaml
 namespace: aaaa
name: bbb
  description: ccccc
format::
  type: sample
  file: foo.sh

main.go
package main

import (
"fmt"
"io/ioutil"
"os"

)

type Sample struct {
Namespace   string `yaml:"namespace"`
Name        string `yaml:"name"`
Description string `yaml:"description"`
Format      struct {
Type string `yaml:"type"`
File string `yaml:"file"`
} `yaml:"format"`
}

const (
invalidYaml = "./invalid.yaml"
)

func main() {
data, err := ioutil.ReadFile(invalidYaml)
if err != nil {
os.Exit(1)
}
sample := &Sample{}
err = yaml.Unmarshal(data, sample)
fmt.Printf("%v", err)
}


Expect to see
  osX
    <nil>
  linux
    <nil>
 or
  osX
    yaml: line 1: did not find expected <document start>
  linux
    yaml: line 1: did not find expected <document start>
See instead
osX yaml: line 1: did not find expected <document start>
  linux
    <nil>


Sorry Thank you.

roger peppe

unread,
Mar 16, 2018, 10:51:43 AM3/16/18
to dev.ys...@gmail.com, golang-nuts
Could you please create a self-contained example where the
YAML is inside the code itself (use a back-quoted string containing the YAML)?

Also, could you please confirm the git commit hashes of the
YAML you're using on each platform?

If you're using a different version on each platform, please confirm
that your still see the difference in behaviour when you checkout
the same version on each.

thanks,
rog.

Yuichi Sawada

unread,
Mar 16, 2018, 11:24:12 AM3/16/18
to golang-nuts
Thank you for your message. 
I tried to exec with  back-quoted string and check git hash number again.

The code which I ran is
package main

import (
"fmt"

)

type Sample struct {
Namespace   string `yaml:"namespace"`
Name        string `yaml:"name"`
Description string `yaml:"description"`
Format      struct {
Type string `yaml:"type"`
File string `yaml:"file"`
} `yaml:"format"`
}

const invalidYaml = `
 namespace: aaaa
name: bbb
description: ccccc
format::
  type: sample
file: foo.sh
`

func main() {
fmt.Println(invalidYaml)
sample := &Sample{}
err := yaml.Unmarshal([]byte(invalidYaml), sample)
fmt.Printf("%v\n", err)
}

The result in mac is here
% sw_vers; go version; git log --oneline -1; go run main.go                                                                                            (git)-[master]
ProductName:    Mac OS X
ProductVersion: 10.12.6
BuildVersion:   16G1036
go version go1.10 darwin/amd64
201773b (HEAD -> master, origin/master, origin/HEAD) add line break

 namespace: aaaa
name: bbb
description: ccccc
format::
  type: sample
file: foo.sh

yaml: line 2: did not find expected <document start>

The result in linux is here
cat /proc/version; go version; git log --oneline -1; go run main.go
Linux version 3.10.0-693.11.6.el7.x86_64 (bui...@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Thu Jan 4 01:06:37 UTC 2018
go version go1.10 linux/amd64
201773b add line break

 namespace: aaaa
name: bbb
description: ccccc
format::
  type: sample
file: foo.sh

<nil>

Am I answer collect?

Thank you.
Yuichi

roger peppe

unread,
Mar 16, 2018, 2:21:16 PM3/16/18
to Yuichi Sawada, golang-nuts
I was more interested in the specific version of yaml you were using.
What does this command print for you on each platform?

git --git-dir $(go list -f '{{.Dir}}' gopkg.in/yaml.v2)/.git log -1

(I'm assuming you use the bash shell).

thanks.
rog.

Yuichi Sawada

unread,
Mar 16, 2018, 7:23:01 PM3/16/18
to golang-nuts
Thank you for your pointing out. This problem is occur by the difference of yaml veresion.
When I use same version of YAML, I got same results.

In Mac I use

commit d670f9405373e636a5a2765eea47fac0c9bc91a4 (HEAD -> v2, origin/v2, origin/master, origin/HEAD)
Merge: 1244d3c 1f1f618
Author: Roger Peppe <rogp...@gmail.com>
Date:   Tue Jan 9 11:43:31 2018 +0000

    Merge pull request #253 from heldtogether/patch-1

    docs: note need for public struct fields

 In Linux I use
commit 7f97868eec74b32b0982dd158a51a446d1da7eb5
Merge: 3e6d767 49fdd64
Author: Roger Peppe <rogp...@gmail.com>
Date:   Fri Feb 23 19:12:37 2018 +0000

    Merge pull request #336 from rogpeppe/025-go.mod


Really sorry for I am not checking these things and send message and very thank you for your help.
If I meet these thing, firstly I try to create same environments.

Thank you
Yuichi
Reply all
Reply to author
Forward
0 new messages