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

GoとTOML

 GoとTOML

Tatsuhiko Kubo

April 13, 2015
Tweet

More Decks by Tatsuhiko Kubo

Other Decks in Technology

Transcript

  1. ݸਓతʹ΋͏͍͍΍ͱࢥͬͯΔ΍ͭ • XML • ਓ͕ؒॻ͘΋ͷ͡Όͳ͍ • YAML • ਓؒʹ͸ॻ͖΍͍͚͢Ͳ࢓༷͕ෳࡶ •

    JSON • ίϝϯτ͕ॻ͚ͳ͍ • 8ਐ਺͕࢖͑ͳ͍(e.g. ύʔϛογϣϯͱ͔) • ຤ඌͷΧϯϚফ͠๨ΕͯγϯλοΫεΤϥʔorz • σʔλϑΥʔϚοτͱͯ͠͸༏ल͕ͩઃఆϑΝΠϧͱͯ͠͸ͭΒ͍
  2. 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"
  3. 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ϥΠΫͳΞϊςʔγϣϯ
  4. 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"` }
  5. TOMLϑΝΠϧͷϩʔυ func LoadConf(confPath string, confToml *ConfToml) error { _, err

    := toml.DecodeFile(confPath, confToml) if err != nil { return err } return nil } ؆୯Ͱ͢Ͷ
  6. 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())