"Go YAML Unmarshal"의 두 판 사이의 차이

 
(같은 사용자의 중간 판 6개는 보이지 않습니다)
10번째 줄: 10번째 줄:
"fmt"
"fmt"


"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)
)


30번째 줄: 30번째 줄:
`)
`)
var colorGroup ColorGroup
var colorGroup ColorGroup
err := yaml.Unmarshal(bytes, &colorGroup)
if err := yaml.Unmarshal(bytes, &colorGroup); err != nil {
if err != nil {
fmt.Println("error:", err)
fmt.Println("error:", err)
}
}
43번째 줄: 42번째 줄:


import (
import (
        "fmt"
"fmt"
        "gopkg.in/yaml.v2"
 
"gopkg.in/yaml.v3"
)
)


65번째 줄: 65번째 줄:
}
}
fmt.Printf("%+v", animals)
fmt.Printf("%+v", animals)
}
</syntaxhighlight>
==구조체 슬라이스 Rules==
<syntaxhighlight lang='go' run>
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v3"
)
type Rule struct {
APIGroups      []string `yaml:"apiGroups"`
Resources      []string `yaml:"resources"`
ResourceNames  []string `yaml:"resourceNames"`
NonResourceURLs []string `yaml:"nonResourceURLs"`
Verbs          []string `yaml:"verbs"`
}
type Rules []Rule
var data = `
- apiGroups: [""]
  resources: ["pods", "pods/log"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["coordination.k8s.io"]
  resourceNames: ["kube-scheduler"]
  resources: ["leases"]
  verbs: ["get", "update"]
- nonResourceURLs: ["/metrics"]
  verbs: ["get"]
`
func main() {
rules := Rules{}
err := yaml.Unmarshal([]byte(data), &rules)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("%+v\n", rules)
}
}
</syntaxhighlight>
</syntaxhighlight>
72번째 줄: 114번째 줄:
* [[Go YAML Marshal()]]
* [[Go YAML Marshal()]]
* [[Go JSON Unmarshal()]]
* [[Go JSON Unmarshal()]]
* [[Go 구조체 포인터에 Unmarshal]]
* [[Go YAML 구조체 없이 맵에 Unmarshal]]


[[분류: Go YAML]]
[[분류: Go YAML]]

2024년 7월 6일 (토) 11:57 기준 최신판

1 개요[ | ]

Go YAML Unmarshal()
  • YAML → 구조체

2 예시: ColorGroup[ | ]

package main

import (
	"fmt"

	"gopkg.in/yaml.v3"
)

type ColorGroup struct {
	ID     int
	Name   string
	Colors []string
}

func main() {
	var bytes = []byte(`
id: 1
name: Reds
colors:
- Crimson
- Red
- Ruby
- Maroon
`)
	var colorGroup ColorGroup
	if err := yaml.Unmarshal(bytes, &colorGroup); err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v", colorGroup)
}

3 예시: Animals[ | ]

package main

import (
	"fmt"

	"gopkg.in/yaml.v3"
)

type Animal struct {
	Name  string
	Order string
}

func main() {
	var bytes = []byte(`
- name: Platypus
  order: Monotremata
- name: Quoll
  order: Dasyuromorphia
`)
	var animals []Animal
	err := yaml.Unmarshal(bytes, &animals)
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v", animals)
}

4 구조체 슬라이스 Rules[ | ]

package main

import (
	"fmt"
	"log"

	"gopkg.in/yaml.v3"
)

type Rule struct {
	APIGroups       []string `yaml:"apiGroups"`
	Resources       []string `yaml:"resources"`
	ResourceNames   []string `yaml:"resourceNames"`
	NonResourceURLs []string `yaml:"nonResourceURLs"`
	Verbs           []string `yaml:"verbs"`
}
type Rules []Rule

var data = `
- apiGroups: [""]
  resources: ["pods", "pods/log"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["coordination.k8s.io"]
  resourceNames: ["kube-scheduler"]
  resources: ["leases"]
  verbs: ["get", "update"]
- nonResourceURLs: ["/metrics"]
  verbs: ["get"]
`

func main() {
	rules := Rules{}
	err := yaml.Unmarshal([]byte(data), &rules)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("%+v\n", rules)
}

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}