go-ini
Kho lưu trữ: go-ini/ini: Package ini provides INI file read and write functionality in Go (github.com)
Tài liệu: go-ini/ini: Thư viện thao tác file INI tuyệt vời cho Go (unknwon.cn)
Giới thiệu
Thư viện phân tích cú pháp file ini được viết bằng Go, hỗ trợ tuần tự hóa và phản tuần tự hóa, hỗ trợ ánh xạ struct, hỗ trợ thao tác chú thích.
Cài đặt
go get gopkg.in/ini.v1Bắt đầu nhanh
File ini như sau
ini
# possible values : production, development
app_mode = development
[paths]
# Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used)
data = /home/git/grafana
[server]
# Protocol (http or https)
protocol = http
# The http port to use
http_port = 9999
# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
enforce_domain = trueFile go
go
package main
import (
"fmt"
"os"
"gopkg.in/ini.v1"
)
func main() {
cfg, err := ini.Load("my.ini")
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
// Thao tác đọc điển hình, có thể sử dụng chuỗi rỗng cho phân vùng mặc định
fmt.Println("App Mode:", cfg.Section("").Key("app_mode").String())
fmt.Println("Data Path:", cfg.Section("paths").Key("data").String())
// Chúng ta có thể thực hiện một số thao tác giới hạn giá trị ứng viên
fmt.Println("Server Protocol:",
cfg.Section("server").Key("protocol").In("http", []string{"http", "https"}))
// Nếu giá trị đọc không nằm trong danh sách ứng viên, sẽ quay lại sử dụng giá trị mặc định được cung cấp
fmt.Println("Email Protocol:",
cfg.Section("server").Key("protocol").In("smtp", []string{"imap", "smtp"}))
// Thử chuyển đổi kiểu tự động
fmt.Printf("Port Number: (%[1]T) %[1]d\n", cfg.Section("server").Key("http_port").MustInt(9999))
fmt.Printf("Enforce Domain: (%[1]T) %[1]v\n", cfg.Section("server").Key("enforce_domain").MustBool(false))
// Xong rồi, sửa một giá trị nào đó và lưu
cfg.Section("").Key("app_mode").SetValue("production")
cfg.SaveTo("my.ini.local")
}Đầu ra
$ go run main.go
App Mode: development
Data Path: /home/git/grafana
Server Protocol: http
Email Protocol: smtp
Port Number: (int) 9999
Enforce Domain: (bool) true
$ cat my.ini.local
# possible values : production, development
app_mode = production
[paths]
# Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used)
data = /home/git/grafana
...Ánh xạ Struct
go-ini hỗ trợ ánh xạ trực tiếp giữa file ini và struct:
go
type Config struct {
AppMode string `ini:"app_mode"`
DataPath string `ini:"data,omitempty"`
}
type Server struct {
Protocol string `ini:"protocol"`
HttpPort int `ini:"http_port"`
}
func main() {
cfg, err := ini.Load("config.ini")
if err != nil {
panic(err)
}
var config Config
err = cfg.MapTo(&config)
if err != nil {
panic(err)
}
var server Server
err = cfg.Section("server").MapTo(&server)
if err != nil {
panic(err)
}
}Ghi chú thích
go
cfg, err := ini.Load("config.ini")
if err != nil {
panic(err)
}
// Thêm chú thích
cfg.Section("").Key("app_mode").Comment = "Chế độ ứng dụng"
cfg.Section("server").Key("http_port").Comment = "Cổng HTTP"
cfg.SaveTo("config_with_comments.ini")Best Practices
- Sử dụng MapTo: Sử dụng ánh xạ struct để giảm code boilerplate
- Xử lý lỗi: Luôn kiểm tra lỗi khi load và parse file ini
- Default values: Sử dụng MustInt, MustBool, MustString để cung cấp giá trị mặc định
- Bảo mật: Không lưu trữ thông tin nhạy cảm trong file ini
- Encoding: Chú ý đến encoding của file ini để tránh vấn đề với ký tự đặc biệt
Kết luận
go-ini là một thư viện mạnh mẽ và dễ sử dụng cho việc đọc và ghi file ini trong Go. Với hỗ trợ ánh xạ struct, chú thích và nhiều tính năng hữu ích khác, go-ini là lựa chọn tuyệt vời cho việc quản lý cấu hình ứng dụng.
