conf

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2025 License: MIT Imports: 2 Imported by: 8

README

README

Export the env to the shell

export PRJ_ROOT=$HOME/ws/test

Example for RootDir

package db

import (
    "os/filepath"

	"github.com/gwaylib/conf"
	"github.com//gwaylib/qsql"
	_ "github.com/go-sql-driver/mysql"
)

var dbFile = filepath.Join(conf.RootDir(),  "etc/db.cfg")

func DB(section string) *qsql.DB {
	return qsql.CacheDB(dbFile, section)
}

func HasDB(section string) (*qsql.DB, error) {
	return qsql.HasDB(dbFile, section)
}

For realtime ini read

package db

import (
	"github.com/gwaylib/conf"
	"github.com/gwaylib/conf/ini"
)

func main() {
    etcRoot := filepath.Join(conf.RootDir(), "etc")
    etc := ini.NewIni(etcRoot).GetFile("etc.ini") // read disk file every times.
    str := etc.String("test", "str")
    if str != "abc" {
        panic("expect abc, but : " + str)
    }
}

For memory cache ini read

package db

import (
	"github.com/gwaylib/conf"
	"github.com/gwaylib/conf/ini"
)

func main() {
    etcRoot := filepath.Join(conf.RootDir(), "etc")
    // etcCache := ini.NewTimeoutIniCache(etcRoot, 5 * time.Munite) // the cache should reload when loaded after 5*time.Minute
    etcCache := ini.NewCacheIni(etcRoot).GetFile("etc.ini")
    str := etc.String("test", "str")
    if str != "abc" {
        panic("expect abc, but : " + str)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitRootDir added in v0.0.2

func InitRootDir(path string)

func RootDir

func RootDir() string

Types

This section is empty.

Directories

Path Synopsis
ini
cache ini to memory
cache ini to memory

Jump to

Keyboard shortcuts

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