GoͱTOMLTatsuhiko [email protected]Gunosy.go#[email protected]/04/13
View Slide
ࣗݾհ• ٱอୡ(Tatsuhiko Kubo)• [email protected]• Software Engineer in Infrastructure Engineering• Mercari, Inc.
OSS࡞ͬͨΓίϯτϦϏϡʔτͨ͠Γ
AgendaGoͰϓϩάϥϜΛॻ͘ࡍͷઃఆϑΝΠϧʹ͍ͭͯ
Έͳ͞ΜɺઃఆϑΝΠϧʹීஈԿΛͬͯ·͔͢ʁ
JSONYAMLʹINIɺ࠷ۙͩͱTOMLͳΜ͔͋Γ·͢ΑͶ
ࠓճ͕ීஈGoͰ։ൃ͢Δࡍʹར༻͢ΔઃఆϑΝΠϧܗࣜͷTOMLʹ͍࣮ͭͯྫΛ౿·͑ͳ͕Βղઆ͠·͢
ͦͦઃఆϑΝΠϧ͕ඞཁͳཧ༝• ͨ͘͞ΜͷίϚϯυϥΠϯҾ• ϓϩάϥϜͷύϥϝʔλ੍ޚ• ϓϩάϥϜ෦ͷৄࡉ͕Θ͔Βͳ͍ਓͰϓϩάϥϜͷมߋΛՄೳʹ͍ͨ͠
ઃఆهड़ݴޠ(ͱͯ͑ͦ͠͏ͳͷ)• XML• INI• JSON• YAML• TOML• ܰྔݴޠ(Lua, mruby, etc…)
ݸਓతʹ͏͍͍ͱࢥͬͯΔͭ• XML• ਓ͕ؒॻ͘ͷ͡Όͳ͍• YAML• ਓؒʹॻ͖͍͚͢Ͳ༷͕ෳࡶ• JSON• ίϝϯτ͕ॻ͚ͳ͍• 8ਐ͕͑ͳ͍(e.g. ύʔϛογϣϯͱ͔)• ඌͷΧϯϚফ͠ΕͯγϯλοΫεΤϥʔorz• σʔλϑΥʔϚοτͱͯ͠༏ल͕ͩઃఆϑΝΠϧͱͯͭ͠Β͍
ઃఆهड़ݴޠ(ͱͯ͑ͦ͠͏ͳͷ)• INI• TOML• ܰྔݴޠ(Lua, mruby, etc…)
ઃఆهड़ݴޠ(ͱͯ͑ͦ͠͏ͳͷ)• INI• TOML• ܰྔݴޠ(Lua, mruby, etc…)ࠓճ͕࣌ؒͳ͍ͷͰল͖·͢
TOML• ͋ΔҙຯAdvanced-INI• ਓ͕ؒಡΈॻ͖͍͢͠• ྻɺ֊ߏ͕ѻ͑Δ• ࠷͕ۙࣗGoͰ։ൃ͢Δ࣌΄΅͜Ε
TOMLͷGo࣮• ͍Ζ͍Ζ͋Δ͚ͲˣΛͬͯ·͢• github.com/BurntSushi/toml• ઃఆϑΝΠϧͷΛͦͷ··ߏମʹϚοϐϯάՄೳ• encoding/jsonͷMarshalͱUnMarshalΈ͍ͨͳײ͡• TOMLͷઃఆࣗମΛੜ͢Δ͜ͱՄೳ
TOML in Gocachectlͷ߹
cachectl• GoͰॻ͔ΕͨOSͷϖʔδΩϟογϡ֬ೝɾআπʔϧ• https://github.com/cubicdaiya/cachectl• ແବͳϖʔδΩϟογϡΛղ์͢Δͷʹར༻• cachectld͍ͬͯ͏σʔϞϯ͕͍ͯΔ• ͜ͷσʔϞϯͷઃఆϑΝΠϧ͕TOML
cachectld.toml
slackboard• GoͰॻ͔ΕͨSlackϓϩΩγ• https://github.com/cubicdaiya/slackboard• ͜ΕͷઃఆϑΝΠϧTOML
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"
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ϥΠΫͳΞϊςʔγϣϯ
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"`}
TOMLϑΝΠϧͷϩʔυfunc LoadConf(confPath string, confToml *ConfToml) error {_, err := toml.DecodeFile(confPath, confToml)if err != nil {return err}return nil}؆୯Ͱ͢Ͷ
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())
·ͱΊ• TOML͍͍ΑTOML• GoͳΒˣ͕͓͢͢Ί• github.com/BurntSushi/toml• P.S.• հ͢Δ࣌ؒͳ͔͚ͬͨͲgopher-luaͱ͔ྑͦ͞͏