1 개요[ | ]
- Go YAML Unmarshal()
- YAML → 구조체
2 예시: ColorGroup[ | ]
Go
CPU
-1.0s
MEM
-0M
-1.0s
Copy
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)
}
{ID:1 Name:Reds Colors:[Crimson Red Ruby Maroon]}3 예시: Animals[ | ]
Go
Copy
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)
}
Loading
4 구조체 슬라이스 Rules[ | ]
Go
Copy
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)
}
Loading
5 같이 보기[ | ]
편집자 Jmnote
로그인하시면 댓글을 쓸 수 있습니다.