Debian Codegraph @

example/single_document_loader/main.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Example: Single Document Loader demonstrates loading a single YAML document.
5
6 package main
7
8 import (
9 "fmt"
10 "io"
11 "strings"
12
13 "go.yaml.in/yaml/v4"
14 )
15
16 type Config struct {
17 Name string `yaml:"name"`
18 Version string `yaml:"version"`
19 Tags []string `yaml:"tags,omitempty"`
20 }
21
22 func main() {
23 fmt.Println("Example 3: Loader with WithSingleDocument Option")
24
25 multiDoc := `---
26 name: app1
27 version: 1.0.0
28 ---
29 name: app2
30 version: 2.0.0
31 tags:
32 - experimental
33 ---
34 name: app3
35 version: 3.0.0
36 `
37
38 // Create loader that stops after first document
39 loader, err := yaml.NewLoader(strings.NewReader(multiDoc), yaml.WithSingleDocument())
40 if err != nil {
41 panic(err)
42 }
43
44 var firstDoc Config
45 if err := loader.Load(&firstDoc); err != nil {
46 panic(err)
47 }
48 fmt.Printf("First document: %+v\n", firstDoc)
49
50 // Try to load second document
51 var secondDoc Config
52 err = loader.Load(&secondDoc)
53 if err == io.EOF {
54 fmt.Println("Second Load() returned io.EOF (as expected with WithSingleDocument)")
55 } else {
56 fmt.Printf("Unexpected: got %v\n", err)
57 }
58 }