Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of Golang using ini Library to read configuration

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "Golang uses ini library to read configuration". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Partition of go-ini

Multiple configuration items for go-ini are divided by section. There are default (empty) partitions and named partitions. If the partition is not named, it is the default partition. The default partition must be written on top of any named partition. Each configuration item is distinguished by line breaks.

Installation of go-ini

Go-ini/ini is a code base on GitHub. Like other golang libraries, it can be downloaded from the golang command line. If the download speed is slow, try to switch agents: go env-w GOPROXY= https://goproxy.cn

Download command:

Format of the go get github.com/go-ini/iniini file

Ini files are partitioned into multi-block configurations

The default partition must be written on top of any named partition, and multiple configuration items are separated by newline symbols.

The configuration items for reading the ini file are directly added to the ini file and read by the method.

Use ini.load (ini file path). Returns a configuration file structure pointer and error message

Cfgs, err: = ini.Load (". / conf/go-conf.ini") if err! = nil {fmt.Println (err)} / / cfgs is the structure pointer of the configuration file

Read named partition data:

Sp: = cfgs.Section () Select a partition and return the structure pointer of the partition

Key () is a method of partition structures that reads the configuration items of the partition and returns the structure pointer of a key.

Val: = cfgs.Section ("kafka"). Key ("address"). Value () fmt.Print (val) / / print: 127.0.0.1

Read default partition data

The default partition only needs the key partition to be empty: val: = cfgs.Section (). Key (). Value ()

Val: = cfgs.Section (") .Key (" a ") .Value () fmt.Print (val) / / print: 122,

About the read key structure pointer. Key values can be converted to multiple types, including basic data types that are not limited to go, with two return values

Value () returns directly without swapping, with only one return value.

/ / the direct return value val: = cfgs.Section ("). Key (" a "). Value () fmt.Print (val) / / print: 122 val,err / returns a key value of type int val,err: = cfgs.Section ("). Key ("a"). Int () if err! = nil {fmt.Print (err)} fmt.Print (val) / / print: 122 defines a structure to store configuration items Map configuration items to structures

Define the structure that matches the ini configuration item, and map the configuration to the structure through ini.MapTo ().

The member attributes in the structure must be added with the corresponding configuration items mapped by the tag,ini library through tap.

Type KafkaConfig struct {/ / ini is used to identify Address string `ini: "address" `} type TailConfig struct {Path string `ini: "path" `Filename string `ini: "fileName"` / / if it is a structure, specify the partition name. Other specified configuration items can be Children `ini: "tailfile.children" `} type Children struct {Name string `ini: "name"`} type Config struct {KafkaConfig `ini: "kafka" `TailConfig `ini: "tailfile"`} func main () {/ / instantiate the structure first Pass the pointer to the MapTo method var cfg = new (Config) err: = ini.MapTo (cfg, ". / conf/go-conf.ini") if err! = nil {fmt.Print (err)} / / take the deepest configuration item fmt.Println (cfg.TailConfig.Children.Name)} / / run result: pp_mode parent-child partition

Go-ini has parent-child partitions, but the hierarchy is not very clear. Whether in the structure or direct file operation, the operation is the same at the level of peace.

You can use it in the partition name. To represent the parent-child relationship between two or more partitions. If a key does not exist in the child partition, it will look again in its parent partition until there is no parent partition.

As in the previous example, the mapping cfg.TailConfig.Children, tag is marked with tailfile.children, which is exactly the same as the level, and the hierarchy is reflected through the structure.

Two key codes

Main.go

Package mainimport ("fmt"github.com/go-ini/ini") type KafkaConfig struct {Address string `ini: "address" `} type TailConfig struct {Path string `ini: "path" `Filename string` ini: "fileName" `/ / if it is a structure Specify Children `ini: "tailfile.children" `} type Config struct {KafkaConfig `ini: "kafka" `TailConfig `ini: "tailfile"`} type Children struct {Name string `ini: "name" `} func main () {/ / var cfg = new (Config) / / err: = ini.MapTo (cfg) ". / conf/go-conf.ini") / / if err! = nil {/ / fmt.Print (err) /} / / fmt.Println (cfg.TailConfig.Children.Name) cfgs, err: = ini.Load (". / conf/go-conf.ini") if err! = nil {fmt.Println (err)} / / val _: = cfgs.Section (") .Key (" a ") .Int () fmt.Print (val)}

Go-conf.ini

A = 122 [Kafka] address = 127.0.0.1: 9092 [tailfile] path = f:/runtime/tmpfileName = 4.log [tailfile.children] name = pp_mode "how Golang uses the ini library to read configurations" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report