无法读取和打印 YAML 文件数据。

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

Unable to read and print yaml file data

问题

语言:Go

我正在练习如何读取和打印yaml文件的数据,但无法做到,并且代码无法通过。有人可以帮忙吗?

Yaml文件:

ENV:
 foo: test
 boo: test-123-tet222

代码:

package test

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

type config struct {
    foo string 
    boo string 
}


func TestTerraformAzureCosmosDBExample(t *testing.T) {
 yFile, err := ioutil.ReadFile("config.yaml")
 if err != nil {
     log.Fatal(err)
 }

 data := make(map[string]config)

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

期望输出:

 foo: test
 boo: test-123-tet222

实际输出:

C:\foo\boo>go test -v
=== RUN   TestTerraformAzureCosmosDBExample
ENV%!(EXTRA test.config={ })--- PASS: TestTerraformAzureCosmosDBExample (0.00s)
PASS
ok      foobo_test   0.179s
英文:

Language: Go

I am practicing how to read and print the yaml file data but unable to do so and the code is getting through. Can someone help here?

Yaml file:

ENV:
 foo: test
 boo: test-123-tet222

code:

package test

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

type config struct {
    foo string 
    boo string 
}


func TestTerraformAzureCosmosDBExample(t *testing.T) {
 yFile, err := ioutil.ReadFile("config.yaml")
 if err != nil {
     log.Fatal(err)
 }

 data := make(map[string]config)

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

Expected Output:

 foo: test
 boo: test-123-tet222

Actual Output:

C:\foo\boo>go test -v
=== RUN   TestTerraformAzureCosmosDBExample
ENV%!(EXTRA test.config={ })--- PASS: TestTerraformAzureCosmosDBExample (0.00s)
PASS
ok      foobo_test   0.179s

答案1

得分: 2

你的config结构体缺少yaml标签。请按照以下方式进行编辑。另外,Printf方法需要一个格式化字符串,请按照以下方式进行编辑。

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

type config struct {
	Foo string `yaml:"foo"`
	Boo string `yaml:"boo"`
}


func TestTerraformAzureCosmosDBExample(t *testing.T) {
	yFile, err := ioutil.ReadFile("config.yaml")
	if err != nil {
		log.Fatal(err)
	}

	data := make(map[string]config)

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

输出结果为:

key: ENV, value: {test test-123-tet222}--- PASS: TestTerraformAzureCosmosDBExample (0.00s)
英文:

Your config struct is missing yaml tags. Edit it as follows. Also the Printf method expects a formatter string, edit that too as follows.

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

type config struct {
	Foo string `yaml:"foo"`
	Boo string `yaml:"boo"`
}


func TestTerraformAzureCosmosDBExample(t *testing.T) {
	yFile, err := ioutil.ReadFile("config.yaml")
	if err != nil {
		log.Fatal(err)
	}

	data := make(map[string]config)

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

gives the output:

key: ENV, value: {test test-123-tet222}--- PASS: TestTerraformAzureCosmosDBExample (0.00s)

答案2

得分: 1

你正在迭代一个名为datamap[string]config

该对象有一个键ENV,该键的值是你要查找的config对象。

尝试使用以下代码:

fmt.Printf("Foo: %S\n", data["ENV"].Foo)
fmt.Printf("Boo: %S\n", data["ENV"].Boo)
英文:

You are iterating over data that is a map[string]config.

That object has one key, ENV and the value for that key is the config object you are looking for.

Try with:

fmt.Printf("Foo: %S\n", data["ENV"].Foo)
fmt.Printf("Boo: %S\n", data["ENV"].Boo)

huangapple
  • 本文由 发表于 2021年7月27日 15:38:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/68540748.html
匿名

发表评论

匿名网友

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

确定