Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

ࣗݾ঺հ • ٱอୡ඙(Tatsuhiko Kubo) • bokko@cubicdaiya • Software Engineer in Infrastructure Engineering • Mercari, Inc.

Slide 3

Slide 3 text

OSS࡞ͬͨΓίϯτϦϏϡʔτͨ͠Γ

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

ઃఆهड़ݴޠ(ͱͯ͠࢖͑ͦ͏ͳ΋ͷ) • XML • INI • JSON • YAML • TOML • ܰྔݴޠ(Lua, mruby, etc…)

Slide 10

Slide 10 text

ݸਓతʹ΋͏͍͍΍ͱࢥͬͯΔ΍ͭ • XML • ਓ͕ؒॻ͘΋ͷ͡Όͳ͍ • YAML • ਓؒʹ͸ॻ͖΍͍͚͢Ͳ࢓༷͕ෳࡶ • JSON • ίϝϯτ͕ॻ͚ͳ͍ • 8ਐ਺͕࢖͑ͳ͍(e.g. ύʔϛογϣϯͱ͔) • ຤ඌͷΧϯϚফ͠๨ΕͯγϯλοΫεΤϥʔorz • σʔλϑΥʔϚοτͱͯ͠͸༏ल͕ͩઃఆϑΝΠϧͱͯ͠͸ͭΒ͍

Slide 11

Slide 11 text

ઃఆهड़ݴޠ(ͱͯ͠࢖͑ͦ͏ͳ΋ͷ) • INI • TOML • ܰྔݴޠ(Lua, mruby, etc…)

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

TOML in Go cachectlͷ৔߹

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

cachectld.toml

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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"

Slide 20

Slide 20 text

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ϥΠΫͳΞϊςʔγϣϯ

Slide 21

Slide 21 text

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"` }

Slide 22

Slide 22 text

TOMLϑΝΠϧͷϩʔυ func LoadConf(confPath string, confToml *ConfToml) error { _, err := toml.DecodeFile(confPath, confToml) if err != nil { return err } return nil } ؆୯Ͱ͢Ͷ

Slide 23

Slide 23 text

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())

Slide 24

Slide 24 text

·ͱΊ • TOML͍͍ΑTOML • GoͳΒˣ͕͓͢͢Ί • github.com/BurntSushi/toml • P.S. • ঺հ͢Δ࣌ؒͳ͔͚ͬͨͲgopher-luaͱ͔ྑͦ͞͏