LoginSignup
2
1

More than 5 years have passed since last update.

configファイルの作成

Last updated at Posted at 2019-01-09

これだと、configファイルが外出しになっていない。
外出しについては、また別途

main.go
package main

import (
    "fmt"
    "gopkg.in/ini.v1"
)

type ConfigList struct {
    DefaultValue int
    DbName       string
    SQLDriver    string
}

var Config ConfigList

func init() {
    cfg, _ := ini.Load("config/config.ini")
    Config = ConfigList{
        DefaultValue:      cfg.Section("web").Key("defaultvalue ").MustInt(),
        DbName:    cfg.Section("db").Key("name").MustString("example.sql"), // MustStringはデフォルトあり
        SQLDriver: cfg.Section("db").Key("driver").String(),                // Stringはデフォルトなし、
    }
}

func main() {
    fmt.Printf("%T %v\n", Config.DefaultValue, Config.DefaultValue)           // int 1000
    fmt.Printf("%T %v\n", Config.DbName, Config.DbName)       // string stockdata.sql
    fmt.Printf("%T %v\n", Config.SQLDriver, Config.SQLDriver) // string sqlite3
}

config/config.ini
[web]
defaultvalue = 1000

[db]
name = stockdata.sql
driver = sqlite3
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1