Read the config file in Go language! Introducing a simple sample

Official document → https://gopkg.in/go-ini/ini.v1

Install package

go get gopkg.in/go-ini/ini.v1

file organization

.
├── config
│   └── config.go
├── config.ini
└── main.go

Create ini file

config.ini


[api]
api_key = aaaaaa
api_secret = bbbbbbb

[db]
password = ccccc
etc...

It is easy to manage if you divide it into sections like [api] [db] above. There is no need to enclose it in quotation marks.

Create config file

config/config.go


package config

import "gopkg.in/ini.v1"

type ConfigList struct {
	APIKey     string
	APISecret  string
	Password   string
}

var Config ConfigList

func init() {
	cfg, err := ini.Load("config.ini")
	if err != nil {
		//Error handling
	}

	Config = ConfigList{
		APIKey:     cfg.Section("api").Key("api_key").String(),
		APISecret:  cfg.Section("api").Key("api_secret").String(),
		Password:   cfg.Section("db").Key("password").String(),
	}
}

Get the value of config

main.go


package main

import "./config"

func main() {
    Println(config.Config.APIKey)
    Println(config.Config.APISecret)
    Println(config.Config.Password)
}

You can get the value of config like this.

This time I created an ini file, but there is also a method using TOML. I recommend this person's article because it was very easy to understand. →https://qiita.com/futoase/items/fd697a708fcbcee104de

Recommended Posts

Read the config file in Go language! Introducing a simple sample
[GO language] Let's read a YAML file
Read a file in Python with a relative path from the program
Read the file line by line in Python
Read the file line by line in Python
[Python] Read the specified line in the file
I made a kind of simple image processing tool in Go language.
Read a file containing garbled lines in Python
I wrote the hexagonal architecture in go language
How to read a file in a different directory
Create a web server in Go language (net/http) (2)
Various ways to read the last line of a csv file in Python
Replace the named entity in the read text file with a label (using GiNZA)
How to display the modification date of a file in C language up to nanoseconds
Read the csv file and display it in the browser
I want to randomly sample a file in Python
Create a web server in Go language (net / http) (1)
[Sublime Text 2] Always execute a specific file in the project
Save the pystan model and results in a pickle file
Change the standard output destination to a file in Python
A note for embedding the scripting language in a bash script
Note 2 for embedding the scripting language in a bash script
[Note] Import of a file in the parent directory in Python
To write a test in Go, first design the interface
Read the keras mnist sample
Download the file in Python
Read and write a file
Hello World in GO language
Write and read a file
Output the key list included in S3 Bucket to a file
Process the contents of the file in order with a shell script
Created gomi, a trash can tool for rm in Go language
[Unexpectedly known? ] Introducing a real day in the data analysis department
Anyway, the fastest serial communication log is left in a file
How to make a command to read the configuration file with pyramid
Read the standard output of a subprocess line by line in Python
Save the Pydrive authentication file in a different directory from the script
Create a function to get the contents of the database in Go
Get the formula in an excel file as a string in Python
A programming language that young people will need in the future
Count specific strings in a file
Try implementing Yubaba in Go language
Use optinal type-like in Go language
Save the binary file in Python
Create a binary file in Python
Let's touch on the Go language
Post to slack in Go language
The story of the "hole" in the file
Start SQLite in a programming language
Save a YAML-formatted file in PyYAML
Run a simple algorithm in Python
Write tests in GO language + gin
How to create a config file
Do something object-oriented in GO language
Replace the directory name and the file name in the directory together with a Linux command.
A memo organized by renaming the file names in the folder with python
[GO language] Organize the behavior of import in places other than GOPATH
How to specify a .ui file in the dialog / widget GUI in PySide
Read a Python # .txt file for a super beginner in Python with a working .py
View the full path (absolute path) of a file in a directory in Linux Bash
I made a program to check the size of a file in Python