GoとTOML

 GoとTOML

5d74d743eabd2bf7d4d2f68b9d3c727d?s=128

Tatsuhiko Kubo

April 13, 2015
Tweet

Transcript

  1. GoͱTOML Tatsuhiko Kubo@cubicdaiya Gunosy.go#12@2015/04/13

  2. ࣗݾ঺հ • ٱอୡ඙(Tatsuhiko Kubo) • bokko@cubicdaiya • Software Engineer in

    Infrastructure Engineering • Mercari, Inc.
  3. OSS࡞ͬͨΓίϯτϦϏϡʔτͨ͠Γ

  4. Agenda GoͰϓϩάϥϜΛॻ͘ࡍͷઃఆϑΝΠϧʹ͍ͭͯ

  5. Έͳ͞ΜɺઃఆϑΝΠϧʹ͸ ීஈԿΛ࢖ͬͯ·͔͢ʁ

  6. JSON΍YAMLʹINIɺ ࠷ۙͩͱTOML ͳΜ͔΋͋Γ·͢ΑͶ

  7. ࠓճ͸๻͕ීஈGoͰ։ൃ͢Δࡍʹ ར༻͢ΔઃఆϑΝΠϧܗࣜͷTOMLʹ ͍࣮ͭͯྫΛ౿·͑ͳ͕Βղઆ͠·͢

  8. ͦ΋ͦ΋ઃఆϑΝΠϧ͕ඞཁͳཧ༝ • ͨ͘͞ΜͷίϚϯυϥΠϯҾ਺ • ϓϩάϥϜͷύϥϝʔλ੍ޚ • ϓϩάϥϜ಺෦ͷৄࡉ͕Θ͔Βͳ͍ਓͰ΋ϓ ϩάϥϜͷมߋΛՄೳʹ͍ͨ͠

  9. ઃఆهड़ݴޠ(ͱͯ͠࢖͑ͦ͏ͳ΋ͷ) • XML • INI • JSON • YAML •

    TOML • ܰྔݴޠ(Lua, mruby, etc…)
  10. ݸਓతʹ΋͏͍͍΍ͱࢥͬͯΔ΍ͭ • XML • ਓ͕ؒॻ͘΋ͷ͡Όͳ͍ • YAML • ਓؒʹ͸ॻ͖΍͍͚͢Ͳ࢓༷͕ෳࡶ •

    JSON • ίϝϯτ͕ॻ͚ͳ͍ • 8ਐ਺͕࢖͑ͳ͍(e.g. ύʔϛογϣϯͱ͔) • ຤ඌͷΧϯϚফ͠๨ΕͯγϯλοΫεΤϥʔorz • σʔλϑΥʔϚοτͱͯ͠͸༏ल͕ͩઃఆϑΝΠϧͱͯ͠͸ͭΒ͍
  11. ઃఆهड़ݴޠ(ͱͯ͠࢖͑ͦ͏ͳ΋ͷ) • INI • TOML • ܰྔݴޠ(Lua, mruby, etc…)

  12. ઃఆهड़ݴޠ(ͱͯ͠࢖͑ͦ͏ͳ΋ͷ) • INI • TOML • ܰྔݴޠ(Lua, mruby, etc…) ࠓճ͸͕࣌ؒͳ͍ͷͰল͖·͢

  13. TOML • ͋ΔҙຯAdvanced-INI • ਓ͕ؒಡΈॻ͖͠΍͍͢ • ഑ྻɺ֊૚ߏ଄͕ѻ͑Δ • ࠷ۙࣗ෼͕GoͰ։ൃ͢Δ࣌͸΄΅͜Ε

  14. TOMLͷGo࣮૷ • ͍Ζ͍Ζ͋Δ͚ͲˣΛ࢖ͬͯ·͢ • github.com/BurntSushi/toml • ઃఆϑΝΠϧͷ஋Λͦͷ··ߏ଄ମʹϚοϐϯάՄೳ • encoding/jsonͷMarshalͱUnMarshalΈ͍ͨͳײ͡ •

    TOMLͷઃఆࣗମΛੜ੒͢Δ͜ͱ΋Մೳ
  15. TOML in Go cachectlͷ৔߹

  16. cachectl • GoͰॻ͔ΕͨOSͷϖʔδΩϟογϡ֬ೝɾ࡟আπʔϧ • https://github.com/cubicdaiya/cachectl • ແବͳϖʔδΩϟογϡΛղ์͢Δͷʹར༻ • cachectld͍ͬͯ͏σʔϞϯ͕෇͍ͯΔ •

    ͜ͷσʔϞϯͷઃఆϑΝΠϧ͕TOML
  17. cachectld.toml

  18. slackboard • GoͰॻ͔ΕͨSlackϓϩΩγ • https://github.com/cubicdaiya/slackboard • ͜ΕͷઃఆϑΝΠϧ΋TOML

  19. slackboard.toml [core] port = "29800" slack_url = "https://hooks.slack.com/services/..." [[tags]] tag

    = "general" channel = "#general" [[tags]] tag = "random" channel = "#random" username = "slackboard" icon_emoji = ":clipboard:" parse = "full" [log] access_log = "stdout" error_log = "stderr" level = "error" [ui] root = "/var/www/slackboard"
  20. TOMLઃఆ༻ͷߏ଄ମఆٛ(cachectld.toml) type ConfToml struct { Targets []SectionTarget `toml:"targets"` } type

    SectionTarget struct { Path string `toml:"path"` PurgeInterval uint64 `toml:"purge_interval"` Filter string `toml:"filter"` Rate float64 `toml:"rate"` } encoding/jsonϥΠΫͳΞϊςʔγϣϯ
  21. TOMLઃఆ༻ͷߏ଄ମఆٛ(slackboard.toml) type ConfToml struct { Core SectionCore `toml:"core"` Tags []SectionTag

    `toml:"tags"` Log SectionLog `toml:"log"` UI SectionUI `toml:"ui"` } type SectionCore struct { Port string `toml:"port"` SlackURL string `toml:"slack_url"` } type SectionTag struct { Tag string `toml:"tag"` Channel string `toml:"channel"` Username string `toml:"username"` IconEmoji string `toml:"icon_emoji"` Parse string `toml:"parse"` } type SectionLog struct { AccessLog string `toml:"access_log"` ErrorLog string `toml:"error_log"` Level string `toml:"level"` } type SectionUI struct { Root string `toml:"root"` }
  22. TOMLϑΝΠϧͷϩʔυ func LoadConf(confPath string, confToml *ConfToml) error { _, err

    := toml.DecodeFile(confPath, confToml) if err != nil { return err } return nil } ؆୯Ͱ͢Ͷ
  23. TOMLઃఆͷΤΫεϙʔτ var b bytes.Buffer // ݱࡏͷTOMLͷઃఆΛόοϑΝʹॻ͖ग़͢ e := toml.NewEncoder(&b) err

    := e.Encode(confToml) if err != nil { log.Fatal(err.Error()) } // ઃఆϑΝΠϧͷ಺༰Λग़ྗ fmt.Println(b.String())
  24. ·ͱΊ • TOML͍͍ΑTOML • GoͳΒˣ͕͓͢͢Ί • github.com/BurntSushi/toml • P.S. •

    ঺հ͢Δ࣌ؒͳ͔͚ͬͨͲgopher-luaͱ͔ྑͦ͞͏