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
--
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.
namespace: aaaaname: bbb description: cccccformat:: type: sample file: foo.sh
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"`
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
osXyaml: line 1: did not find expected <document start>linuxyaml: line 1: did not find expected <document start>
See insteadosX yaml: line 1: did not find expected <document start>
linux
<nil>
Sorry Thank you.
Thank you for your message.
package main
import ( "fmt"
yaml "gopkg.in/yaml.v2")
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: aaaaname: bbbdescription: cccccformat:: type: samplefile: foo.sh
`
func main() { fmt.Println(invalidYaml) sample := &Sample{} err := yaml.Unmarshal([]byte(invalidYaml), sample) fmt.Printf("%v\n", err)}
% sw_vers; go version; git log --oneline -1; go run main.go (git)-[master]ProductName: Mac OS XProductVersion: 10.12.6BuildVersion: 16G1036go version go1.10 darwin/amd64201773b (HEAD -> master, origin/master, origin/HEAD) add line break
namespace: aaaaname: bbbdescription: cccccformat:: type: samplefile: foo.sh
yaml: line 2: did not find expected <document start>
cat /proc/version; go version; git log --oneline -1; go run main.goLinux 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 2018go version go1.10 linux/amd64201773b add line break
namespace: aaaaname: bbbdescription: cccccformat:: type: samplefile: foo.sh
<nil>
commit d670f9405373e636a5a2765eea47fac0c9bc91a4 (HEAD -> v2, origin/v2, origin/master, origin/HEAD)Merge: 1244d3c 1f1f618Author: 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
commit 7f97868eec74b32b0982dd158a51a446d1da7eb5Merge: 3e6d767 49fdd64Author: Roger Peppe <rogp...@gmail.com>Date: Fri Feb 23 19:12:37 2018 +0000
Merge pull request #336 from rogpeppe/025-go.mod