ini

package
v0.0.0-...-48fba18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2022 License: GPL-3.0 Imports: 7 Imported by: 0

README

简单解析ini配置文件

// config.go

package main

import (
	"github.com/liuq000/go-lib/config/ini"
	"time"
)

type configGlobal struct {
	Host     string
	Port     string
	Username string
	Password string
	DBName   string
	Timeout  time.Duration
}

type configTables struct {
	Field string
	Day   int
}

type Config struct {
	Global *configGlobal
	Tables map[string]configTables
}

func (conf *Config) Parse() []error {

	cfg := ini.Load("", true)

	conf.Global = &configGlobal{
		Host:     cfg.String("global", "host"),
		Port:     cfg.String("global", "port"),
		Username: cfg.String("global", "username"),
		Password: cfg.String("global", "password"),
		DBName:   cfg.String("global", "dbname"),
		Timeout:  cfg.Duration("global", "timeout"),
	}
	conf.Tables = make(map[string]configTables)
	tablesName := cfg.SectionStrings()
	for i := range tablesName {
		conf.Tables[tablesName[i]] = configTables{
			Field: cfg.String(tablesName[i], "field"),
			Day:   cfg.Int(tablesName[i], "day"),
		}
	}

	return cfg.Errors()
}
// main.go

package main

import (
	"log"
	"os"
)

func main() {

	conf := &Config{}
	if errs := conf.Parse(); len(errs) != 0 {
		for i := range errs {
			log.Println(errs[i])
		}
		os.Exit(1)
	}

	log.Println("conf:", conf)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	// contains filtered or unexported fields
}

func Load

func Load(name string, _default bool) *File

func (*File) Bool

func (f *File) Bool(sectionString, key string) bool

func (*File) Duration

func (f *File) Duration(sectionString, key string) time.Duration

func (*File) Errors

func (f *File) Errors() []error

func (*File) Float64

func (f *File) Float64(sectionString, key string) float64

func (*File) Int

func (f *File) Int(sectionString, key string) int

func (*File) Keys

func (f *File) Keys(sectionString string) []string

func (*File) Sections

func (f *File) Sections() []string

func (*File) String

func (f *File) String(sectionString, key string) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL