Last active
September 25, 2016 21:00
-
-
Save FilBot3/3c89d50bbbb0a20eecd61e56111f7985 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"}]} |
Author
Author
Changing the time.Time formats in the YAML file is what solved this. The suggestion was from a user on StackOverflow, Putu, who stated that my Time format was wrong. and needed to be this:
"2012-10-31T15:50:13.793654Z"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I comment out Line 41 out of go_yaml_practice.go, this works and prints what I want it to. However, the Slice is not being populated.