我正在尝试使用GO读取YAML文件并将其映射到我定义的结构。 The YAML is below:
--- # go_time_tracker.yml
owner: "Phillip Dudley"
initialized: "2012-10-31 15:50:13.793654 +0000 UTC"
time_data:
- action: "start"
time: "2012-10-31 15:50:13.793654 +0000 UTC"
- action: "stop"
time: "2012-10-31 16:00:00.000000 +0000 UTC"
我使用the following code读取文件,解组数据,然后打印一些数据。
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"time"
)
type Date_File struct {
Owner string `yaml:"owner"`
Init time.Time `yaml:"initialized"`
TimeData []Time_Data `yaml:"time_data"`
}
type Time_Data struct {
//
Action string `yaml:"action"`
Time time.Time `yaml:"time"`
}
func checkerr(err error) {
if err != nil {
log.Fatal(err)
}
}
func read() (td *Date_File) {
//td := &Date_File{}
gtt_config, err := ioutil.ReadFile("go_time_tracker.yml")
checkerr(err)
err = yaml.Unmarshal(gtt_config, &td)
return td
}
func main() {
//
time_data := read()
fmt.Println(time_data)
fmt.Println(time_data.TimeData[0])
fmt.Println(time_data.Owner)
}
当我运行它时,第一个fmt.Println(time_data)
工作,显示引用及其数据。下一行虽然没有说索引超出范围。 This is the error:
$ go run yaml_practice_2.go
&{Phillip Dudley 0001-01-01 00:00:00 +0000 UTC []}
panic: runtime error: index out of range
goroutine 1 [running]:
panic(0x559840, 0xc82000a0e0)
/usr/lib/go-1.6/src/runtime/panic.go:481 +0x3e6
main.main()
/home/predatorian/Documents/go/src/predatorian/yaml/yaml_practice_2.go:41 +0x2aa
exit status 2
然后我想也许我的YAML格式不正确,所以我将YAML文件加载到Ruby的IRB和this is what I got。
irb(main):004:0> data2 = YAML.load(File.read("go_time_tracker.yml"))
=> {"owner"=>"Phillip Dudley", "initialized"=>"2012-10-31 15:50:13.793654 +0000 UTC", "time_data"=>[{"action"=>"start", "time"=>"2012-10-31 15:50:13.793654 +0000 UTC"}, {"action"=>"stop", "time"=>"2012-10-31 16:00:00.000000 +0000 UTC"}]}
IRB输出显示我的YAML格式正确,但是,我认为我没有正确地解组它。但是,我不确定我需要做些什么才能让它发挥作用。我确定我没有考虑如何正确地做到这一点,因为Ruby隐藏了很多。
答案 0 :(得分:3)
首先,在
之后添加checkerr(err)
err = yaml.Unmarshal(gtt_config, &td)
您将收到相应的错误time parsing error
。 yaml
解码器需要RFC3339格式的时间。有几种方法可以解决这个问题:
"2012-10-31T15:50:13.793654Z"
对于(2),我想到了两个解决方案:
yaml.Unmarshaler
界面或time.Time
换行为自定义类型,然后实施encoding.TextUnmarshaler
interface 解决方案(1):您需要为Date_File
和Time_Data
类型实现自定义Unmarshaler。将以下内容添加到源代码中。
func (df *Date_File) UnmarshalYAML(unmarshal func(interface{}) error) error {
//Unmarshal time to string then convert to time.Time manually
var tmp struct {
Owner string `yaml:"owner"`
Init string `yaml:"initialized"`
TimeData []Time_Data `yaml:"time_data"`
}
if err := unmarshal(&tmp); err != nil {
return err;
}
const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
tm, err := time.Parse(layout, tmp.Init)
if err != nil {
return err
}
df.Owner = tmp.Owner
df.Init = tm
df.TimeData = tmp.TimeData
return nil
}
func (td *Time_Data) UnmarshalYAML(unmarshal func(interface{}) error) error {
//Unmarshal time to string then convert to time.Time manually
var tmp struct {
Action string `yaml:"action"`
Time string `yaml:"time"`
}
if err := unmarshal(&tmp); err != nil {
return err;
}
const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
tm, err := time.Parse(layout, tmp.Time)
if err != nil {
return err
}
td.Action = tmp.Action
td.Time = tm
return nil
}
如果您有types
个time.Time
字段,那么解决方案(1)可能不切实际。
解决方案(2):如果查看yaml decoder的源代码,它依赖于TextUnmarshaler
将字符串转换为相应的类型。在这里你需要:
CustomTime
)time.Time
CustomTime
字段
UnmarshalText
(3)的片段:
type CustomTime struct {
time.Time
}
func (tm *CustomTime) UnmarshalText(text []byte) error {
const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
tmValue, err := time.Parse(layout, string(text))
if err != nil {
return err
}
tm.Time = tmValue
return nil
}
//Not directly related, for print function etc.
func (tm CustomTime) String() string {
return tm.Time.String()
}