在Go中无法将!!seq解组为字符串。

huangapple go评论138阅读模式
英文:

cannot unmarshal !!seq into string in Go

问题

在使用 YAML 文件中的数组序列时,我遇到了以下错误。由于我对 'Go' 不熟悉并且正在学习该程序,所以我不确定需要应用什么修复。

2021/07/28 14:16:07 yaml: unmarshal errors:
line 3: cannot unmarshal !!seq into string

YAML:

ENV:
  attributes:
    - foo
    - boo
    - coo

代码:

package test

import (
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
"testing"
)

type ENV struct {
Attributes string yaml:"attributes"
}

func TestTerraformAzureCosmosDBExample(t *testing.T) {

yFile, err := ioutil.ReadFile("config.yaml")


if err != nil {
    log.Fatal(err)
}

data := make(map[string]ENV)

err = yaml.Unmarshal(yFile, &data)
if err != nil {
    log.Fatal(err)
}
for k, v := range data {
    fmt.Printf(`key: %v, value: %v`, k,v)
}

}

期望输出:

foo
boo
coo

实际输出:

C:\Pfoo\boo>go test -v
=== RUN TestTerraformAzureCosmosDBExample
2021/07/28 14:16:07 yaml: unmarshal errors:
line 3: cannot unmarshal !!seq into string
exit status 1
FAIL foo_test 0.199s

英文:

I am getting the following error while using sequence of array in yaml file. I am not sure what fix required to be applied as I am new to 'Go' and learning the program.

2021/07/28 14:16:07 yaml: unmarshal errors:
  line 3: cannot unmarshal !!seq into string

Yaml:

ENV:
  attributes:
    - foo
    - boo
    - coo

code:

package test

import (
    "fmt"
    "gopkg.in/yaml.v3"
    "io/ioutil"
    "log"
    "testing"
)

type ENV struct {
    Attributes string `yaml:"attributes"`
}


func TestTerraformAzureCosmosDBExample(t *testing.T) {


    yFile, err := ioutil.ReadFile("config.yaml")


    if err != nil {
        log.Fatal(err)
    }

    data := make(map[string]ENV)

    err = yaml.Unmarshal(yFile, &data)
    if err != nil {
        log.Fatal(err)
    }
    for k, v := range data {
        fmt.Printf(`key: %v, value: %v`, k,v)
    }

}

Expected:

foo
boo
coo

Actual:

C:\Pfoo\boo>go test -v
=== RUN   TestTerraformAzureCosmosDBExample
2021/07/28 14:16:07 yaml: unmarshal errors:
  line 3: cannot unmarshal !!seq into string
exit status 1
FAIL    foo_test   0.199s

答案1

得分: 4

根据mkopriva在评论中的建议,你应该将代码改为[]string,这样结构体将变为:

type ENV struct {
	Attributes []string `yaml:"attributes"`
}

为什么要这样做呢?因为yaml将ENV: attributes: - foo - boo - coo识别为一个数组。如果你想将其转换为一个字符串,可以使用strings.Join(String Slice, "分隔符")函数。

需要导入的包是"strings",而strings.Join函数返回一个字符串。

英文:

As mkopriva, said in the comments, you should change to []string, so the struct would be

type ENV struct {
	Attributes []string `yaml:"attributes"`
}

And why is that ? the yaml, reconizes the
ENV: attributes: - foo - boo - coo as a array.
What you can do to turn into one string, is use:
strings.Join(String Slice,{ something to separate, you can let this as "")`

the imports are : "strings", and strings.Join returns a string.

答案2

得分: 1

根据mkopriva的建议,将Attributes字段从string更改为string[]

package test

import (
	"fmt"
	"gopkg.in/yaml.v3"
	"io/ioutil"
	"log"
	"testing"
)

type ENV struct {
	Attributes []string `yaml:"attributes"`
}

func TestTerraformAzureCosmosDBExample(t *testing.T) {

	yFile, err := ioutil.ReadFile("config.yaml")

	if err != nil {
		log.Fatal(err)
	}

	data := make(map[string]ENV)

	err = yaml.Unmarshal(yFile, &data)
	if err != nil {
		log.Fatal(err)
	}
	for k, v := range data {
		fmt.Printf("key: %v, value: %v", k, v)
	}

}
英文:

As per suggestion of mkopriva changing Attributes field to string[] instead of string:

package test

import (
	"fmt"
	"gopkg.in/yaml.v3"
	"io/ioutil"
	"log"
	"testing"
)

type ENV struct {
	Attributes []string `yaml:"attributes"`
}

func TestTerraformAzureCosmosDBExample(t *testing.T) {

	yFile, err := ioutil.ReadFile("config.yaml")

	if err != nil {
		log.Fatal(err)
	}

	data := make(map[string]ENV)

	err = yaml.Unmarshal(yFile, &data)
	if err != nil {
		log.Fatal(err)
	}
	for k, v := range data {
		fmt.Printf(`key: %v, value: %v`, k, v)
	}

}

答案3

得分: 1

这是我做的一个示例,希望能有所帮助。

package main

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"log"
)

type Image struct {
	Name  string `yaml:"name"`
	Image string `yaml:"image"`
}

type Specification struct {
	Spec struct {
		Container []Image `yaml:"containers,flow"`
	}
}

func main() {
	container := Specification{}

	content, err := ioutil.ReadFile("final-result.yml")
	if err != nil {
		log.Fatal(err.Error())
		return
	}

	err = yaml.Unmarshal(content, &container)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	log.Println(container)
	for _, ct := range container.Spec.Container {
		fmt.Println(ct.Name)
	}

}
spec:
  containers:
    - name: nginx
      image: nginx:1.14.2
    - name: php
      image: php:1.4.8
    - name: golang
      image: golang:1.4.3

输出结果:

nginx
php
golang
英文:

Here is an example I did. Hope it helps

package main

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"log"
)

type Image struct {
	Name  string `yaml:"name"`
	Image string `yaml:"image"`
}

type Specification struct {
	Spec struct {
		Container []Image `yaml:"containers,flow"`
	}
}

func main() {
	container := Specification{}

	content, err := ioutil.ReadFile("final-result.yml")
	if err != nil {
		log.Fatal(err.Error())
		return
	}

	err = yaml.Unmarshal(content, &container)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	log.Println(container)
	for _, ct := range container.Spec.Container {
		fmt.Println(ct.Name)
	}

}
spec:
  containers:
    - name: nginx
      image: nginx:1.14.2
    - name: php
      image: php:1.4.8
    - name: golang
      image: golang:1.4.3

Output

nginx
php
golang

huangapple
  • 本文由 发表于 2021年7月28日 22:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/68562229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定