This is the 31st day of my participation in the August More Text Challenge

Introduction to the

INI is a Go language INI file manipulation library, supporting various file formats, including but not limited to.ini.cnf.gitconfig, etc

The ini package is used to read configuration files

Officials also specially made a Chinese mirror station, convenient for students in the Chinese section to view documents, quickly start

Features:

  • Support for overloading multiple data sources (file,[]byte.io.Reader and io.ReadCloser)
  • Recursive reading of key values is supported
  • Reading parent and child partitions is supported
  • Support reading from increment key names
  • Supports reading multiple row key values
  • Support for a large number of helper methods
  • Supports direct conversion to the Go language type at read time
  • Support for reading and writing annotations for partitions and keys
  • Easily manipulate partitions, keys, and annotations
  • Partitions and keys remain in the same order when saving the file

INI official portal: github.com/go-ini/ini

The installation

Because it’s a Go package, you can download it in advance, you can introduce resynchronization in your code editor,

To download it in advance, run the following command:

go get gopkg.in/ini.v1
Copy the code

Code editor introduced

import (
   "gopkg.in/ini.v1"
)
Copy the code

use

First we create a config.ini configuration file that looks like this:

app_mode = development
[mysql]
host = ip
port = 3306
dbname =  curdDemo
username = root
password = 666666
conf = charset=utf8mb4&parseTime=True&loc=Local
Copy the code

A config.go file is then created to read the configuration file and assign the value to the global variable

Use the init function to load the configuration file at project startup

var (
   MyHost string
   MyPort string
   MyDB   string
   MyUser string
   MyPass string
   MyConf string
   MyPath = MyHost + MyPort
)
func init() {
   cfg, err := ini.Load("config.ini")
   iferr ! = nil { log.Fatalln(err) } fmt.Println(cfg.Section("").Key("app_mode"))
   MyHost = cfg.Section("mysql").Key("host").String()
   MyPort = cfg.Section("mysql").Key("port").String()
   MyDB = cfg.Section("mysql").Key("dbname").String()
   MyUser = cfg.Section("mysql").Key("username").String()
   MyPass = cfg.Section("mysql").Key("password").String()
   MyConf = cfg.Section("mysql").Key("conf").String()
}
Copy the code

App_mode is not used here, indicating global use

The init package calls the Load method, returns a File structure, and then reads the configuration File K /v using the Section method inside the File structure

All of the above code variables start with a capital letter for cross-package invocation to facilitate use in other package paths


These configuration files are then used and called directly from the method

MyUser and other variables starting with My are all variable names defined in the above code

Although mysql is used as an example, if redis, Mongo, etc., the operation is the same

func SetupDb() *gorm.DB {
   dsn := config.MyUser + ":" + config.MyPass + "@tcp(" + config.MyHost + ":" + config.MyPort + "/" + config.MyDB + "?" + config.MyConf
   Db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
   iferr ! = nil { log.Fatalf("Database connection failed, error log: v%", err)
   }
   log.Println(Mysql connection successful, configuration info: v%,dsn)
   Db.AutoMigrate(&model.User{})

   return Db
}
Copy the code

Now that the configuration is read and used, you can see the power and simplicity of the INI package

In addition to INI package, there are many configuration packages commonly used in Go, including Viper, Toml and GoDotEnv. See the following figure for all configuration packages: