[GO言語] interface {}型を使ってYAMLファイルを読み込んでみる
YAMLとは
環境
- CentOS 8.2
- go version 1.15.3 linux/amd64
準備
# go get gopkg.in/yaml.v2
# vi test.yml
test.yml
first: test
secound:
a1: count1
a2:
- count2
- count3
a3:
b1: count4
YAMLファイルを読み込む
# vi main.go
main.go
package main
import(
"fmt"
"io/ioutil"
"strconv"
"gopkg.in/yaml.v2"
)
func main() {
// test.ymlを読み込む
buf, err := ioutil.ReadFile("test.yml")
if err != nil {
fmt.Print("error: Failed to read the file\n")
return
}
// 読み込んだファイルを map[interface {}]interface {}にマッピングする
t := make(map[interface {}]interface {})
err = yaml.Unmarshal(buf, &t)
if err != nil {
panic(err)
}
fmt.Print(t["first"]) // test
fmt.Print("\n")
// t["secound"]を(map[interface {}]interface {})で型変換
fmt.Print(t["secound"].(map[interface {}]interface {})["a1"]) //count1
fmt.Print("\n")
// len() 配列の要素数を返す
fmt.Print(len(t["secound"].(map[interface {}]interface {})))
fmt.Print("\n")
// []interface {}型の配列
fmt.Print(t["secound"].(map[interface {}]interface {})["a2"].([]interface {})[0]) // count2
fmt.Print("\n")
fmt.Print(t["secound"].(map[interface {}]interface {})["a3"].(map[interface {}]interface {})["b1"]) // count4
fmt.Print("\n")
// 規則的に命名されていれば、いくつあるか確認できる
flag, i := 0, 0
for flag == 0 {
i++
switch t["secound"].(map[interface {}]interface {})["a"+strconv.Itoa(i)].(type) {
case nil:
flag = 1
}
}
fmt.Printf("a%d is not found\n", i) // a4 is not found
}
# go run main.go
test
count1
3
count2
count4
a4 is not found
参考文献
- CentOS 8.2
- go version 1.15.3 linux/amd64
準備
# go get gopkg.in/yaml.v2
# vi test.yml
test.yml
first: test
secound:
a1: count1
a2:
- count2
- count3
a3:
b1: count4
YAMLファイルを読み込む
# vi main.go
main.go
package main
import(
"fmt"
"io/ioutil"
"strconv"
"gopkg.in/yaml.v2"
)
func main() {
// test.ymlを読み込む
buf, err := ioutil.ReadFile("test.yml")
if err != nil {
fmt.Print("error: Failed to read the file\n")
return
}
// 読み込んだファイルを map[interface {}]interface {}にマッピングする
t := make(map[interface {}]interface {})
err = yaml.Unmarshal(buf, &t)
if err != nil {
panic(err)
}
fmt.Print(t["first"]) // test
fmt.Print("\n")
// t["secound"]を(map[interface {}]interface {})で型変換
fmt.Print(t["secound"].(map[interface {}]interface {})["a1"]) //count1
fmt.Print("\n")
// len() 配列の要素数を返す
fmt.Print(len(t["secound"].(map[interface {}]interface {})))
fmt.Print("\n")
// []interface {}型の配列
fmt.Print(t["secound"].(map[interface {}]interface {})["a2"].([]interface {})[0]) // count2
fmt.Print("\n")
fmt.Print(t["secound"].(map[interface {}]interface {})["a3"].(map[interface {}]interface {})["b1"]) // count4
fmt.Print("\n")
// 規則的に命名されていれば、いくつあるか確認できる
flag, i := 0, 0
for flag == 0 {
i++
switch t["secound"].(map[interface {}]interface {})["a"+strconv.Itoa(i)].(type) {
case nil:
flag = 1
}
}
fmt.Printf("a%d is not found\n", i) // a4 is not found
}
# go run main.go
test
count1
3
count2
count4
a4 is not found
参考文献
# go get gopkg.in/yaml.v2
# vi test.yml
test.yml
first: test
secound:
a1: count1
a2:
- count2
- count3
a3:
b1: count4
# vi main.go
main.go
package main
import(
"fmt"
"io/ioutil"
"strconv"
"gopkg.in/yaml.v2"
)
func main() {
// test.ymlを読み込む
buf, err := ioutil.ReadFile("test.yml")
if err != nil {
fmt.Print("error: Failed to read the file\n")
return
}
// 読み込んだファイルを map[interface {}]interface {}にマッピングする
t := make(map[interface {}]interface {})
err = yaml.Unmarshal(buf, &t)
if err != nil {
panic(err)
}
fmt.Print(t["first"]) // test
fmt.Print("\n")
// t["secound"]を(map[interface {}]interface {})で型変換
fmt.Print(t["secound"].(map[interface {}]interface {})["a1"]) //count1
fmt.Print("\n")
// len() 配列の要素数を返す
fmt.Print(len(t["secound"].(map[interface {}]interface {})))
fmt.Print("\n")
// []interface {}型の配列
fmt.Print(t["secound"].(map[interface {}]interface {})["a2"].([]interface {})[0]) // count2
fmt.Print("\n")
fmt.Print(t["secound"].(map[interface {}]interface {})["a3"].(map[interface {}]interface {})["b1"]) // count4
fmt.Print("\n")
// 規則的に命名されていれば、いくつあるか確認できる
flag, i := 0, 0
for flag == 0 {
i++
switch t["secound"].(map[interface {}]interface {})["a"+strconv.Itoa(i)].(type) {
case nil:
flag = 1
}
}
fmt.Printf("a%d is not found\n", i) // a4 is not found
}
# go run main.go
test
count1
3
count2
count4
a4 is not found
参考文献
Author And Source
この問題について([GO言語] interface {}型を使ってYAMLファイルを読み込んでみる), 我々は、より多くの情報をここで見つけました https://qiita.com/naoki-iso/items/71b3fba04bdc21853aea著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .