tomlファイルをgolangから読み込む
以下のpackageを使用して、golangプログラムからtomlファイルを読み込んでみる。
コード
- 実行ファイル
package main import ( "fmt" "os" "io/ioutil" "github.com/BurntSushi/toml" ) //Config 設定ファイル type Config struct { User UserConfig } //UserConfig 設定ファイルのユーザ部分 type UserConfig struct { Name string Age int } var config Config func main() { _, err := toml.DecodeFile("./config.toml", &config) if err != nil { fmt.Println(err) } fmt.Println(config.User.Name) }
- tomlファイル
// config.toml [User] name = "misuzu" age = 0
とっても簡単♪