Created
July 10, 2014 16:39
-
-
Save r9y9/7af297563fd66d6bd64b to your computer and use it in GitHub Desktop.
Parsing yaml in Go
This file contains 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.v1" | |
"io/ioutil" | |
"log" | |
) | |
type Config struct { | |
DataPath string "data_path" | |
LimitFrames string "limit_frames" | |
Ignore0thOrder bool "ignore_0th_order" | |
AddDynamicFeature bool "add_dynamic_feature" | |
} | |
func main() { | |
byte, err := ioutil.ReadFile("config.yaml") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Mapping to Go struct | |
c := Config{} | |
yerr := yaml.Unmarshal(byte, &c) | |
if yerr != nil { | |
log.Fatal(yerr) | |
} | |
fmt.Println("--- Go struct\n", c) | |
d, ymerr := yaml.Marshal(&c) | |
if ymerr != nil { | |
log.Fatal(ymerr) | |
} | |
fmt.Printf("--- dump:\n%s", string(d)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment