yaml格式文件
Account:
Address: ge1yy3ygsfxl8t4gdnwtqfrn62pnl50nn72xayj8a
Pubkey:
Coins: 10000499300000000stake
AccountNumber: 11
Sequence: 4
struct结构体
type Config struct {
Accounts Account `yaml:"Account"`
}
type Account struct {
Address interface{} `yaml:"Address"`
PubKey interface{} `yaml:"PubKey"`
Coins interface{} `yaml:"Coins"`
AccountNumber interface{} `yaml:"AccountNumber"`
Sequence interface{} `yaml:"Sequence"`
}
完整代码
import (
"os/exec"
"fmt"
"gopkg.in/yaml.v2"
)
type Config struct {
Accounts Account `yaml:"Account"`
}
type Account struct {
Address interface{} `yaml:"Address"`
PubKey interface{} `yaml:"PubKey"`
Coins interface{} `yaml:"Coins"`
AccountNumber interface{} `yaml:"AccountNumber"`
Sequence interface{} `yaml:"Sequence"`
}
var out=`
Account:
Address: ge1yy3ygsfxl8t4gdnwtqfrn62pnl50nn72xayj8a
Pubkey:
Coins: 10000499300000000stake
AccountNumber: 11
Sequence: 4
package main
`
func main() {
var b map[string]map[string]interface{}
var cc Config
//通过map解析
err = yaml.Unmarshal(out,&b)
if err != nil {
fmt.Println(err)
return
}
//通过结构体解析
err = yaml.Unmarshal(out,&cc)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(out))
fmt.Printf("%v\n", b["Account"]["Coins"])
fmt.Printf("%v\n", cc.Accounts.Coins)
}