2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Golang】サードパーティー config(go-ini)

Posted at

#【Golang】サードパーティー config(go-ini)
Golangの基礎学習〜Webアプリケーション作成までの学習を終えたので、復習を兼ねてまとめていく。 基礎〜応用まで。

package main

/*
iniでConfigの設定ファイルを読み込む。
設定ファイルを作成する。

インストール 済
コンフィグのファイルを読み込む為のパッケージ
https://godoc.org/gopkg.in/go-ini/ini.v1
go get "gopkg.in/go-ini/ini.v1"

congig.iniを作成する
下記、よくある設定
[web]
port = 8080

[db]
name = test.sql
driver = sqlite3

これをGoで読み込む。osopenなんかも使えるが、iniの方が便利
*/

import (
	"fmt"
	//config
	"gopkg.in/go-ini/ini.v1"
	//"gopkg.in/ini.v1"
)

type ConfigList struct {
	Port      int
	DbName    string
	SQLDriver string
}

var Config ConfigList

func init() {
	cfg, _ := ini.Load("config.ini")

	Config = ConfigList{
		Port:      cfg.Section("web").Key("port").MustInt(),
		DbName:    cfg.Section("db").Key("name").MustString("example.sql"),
		SQLDriver: cfg.Section("db").Key("driver").String(),
	}
}

func main() {
	fmt.Printf("%T %v\n", Config.Port, Config.Port)
	fmt.Printf("%T %v\n", Config.DbName, Config.DbName)
	fmt.Printf("%T %v\n", Config.SQLDriver, Config.SQLDriver)
	/*
			int 8000
		    string example.sql
		    string
	*/
}

2
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?