Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Configuration language for Go is any good?

linyows
September 16, 2014

Configuration language for Go is any good?

TL;DR; HCLを使え

linyows

September 16, 2014
Tweet

More Decks by linyows

Other Decks in Technology

Transcript

  1. JSON { "foo": { "bar": "hello", "baz": { "key": 7,

    "hoge": true, "values": [0, 1, 2] } } } • なんか冗長すぎる • コメントが書けない • Structにtag書くの面倒 • 記述に迷う事はない
  2. YAML foo: bar: hello baz: key: 7 hoge: off on:

    'true' # comment~ values: [0, 1, 2] fuga: !str 123 "999": ~ • シンプルに書ける(コンパクト) • 色々な表現が出来る Anchor/Alias 型宣言 • 逆に敷居が高いのかも… キーにハイフンとか • でもまあ、yamlで良い気がするけど
  3. ͡Ό͋ GoͰ… func MyConfig() *Config { return &Config{ Bar: "hello",

    Baz: BazConfig{ Key: 7, Hoge: true, Values: []int{1, 2, 3}, }, } } • ださい…し、Goの知識がない人はつらい
  4. Grammar hclData := ` a = "Easy!" b { c

    = 2 // comment~ d = [3, 4] e = true } ` variable "foo" { default = "bar" description = "bar" } variable "amis" { default = { east = "foo" } }
  5. Decode type Config struct { A string B struct {

    C int D []int E bool } } hclObject, _ := hcl.Parse(hclData) fmt.Println(hclObject) // &{ 7 [0x2081f0240 0x2081f0390 0x2081f0480 0x2081f0600 0x2081f0690] <nil>} fmt.Println(hclObject.Get("b", false).Get("e", false).Value) // true var c Config hcl.DecodeObject(&c, hclObject) fmt.Println(c) // {Easy! {2 [3 4] true} false}
  6. Grammar hclData := ` A = "Easy!" ! [B] C

    = 2 D = [3, 4] E = true ` title = "TOML Example" ! [owner] name = "Tom Preston-Werner" organization = "GitHub" bio = "GitHub Cofounder & CEO\nLikes tater tots and beer." dob = 1979-05-27T07:32:00Z # First class dates? Why not? ! [database] server = "192.168.1.1" ports = [ 8001, 8001, 8002 ] connection_max = 5000 enabled = true [servers] ! # comment [servers.alpha] ip = "10.0.0.1" dc = "eqdc10" ! [servers.beta] ip = "10.0.0.2" dc = "eqdc10"
  7. Decode type Config struct { A string B struct {

    C int D []int E bool } } var c Config meta, _ := toml.Decode(tomlData, &config) fmt.Println(config) // {Easy! {2 [3 4] true}} fmt.Println(meta) // {map[A:Easy! B:map[D:[3 4] E:true C:2]] map[A:String B:Hash B.C:Integer B.D:Array B.E:Bool] [[A] [B] [B C] [B D] [B E]] map[B.C:true B.D:true B.E:true A:true B:true] []}