GoLang 프로필 읽기
2299 단어 go
[run]
run_dir = /Users/why/Desktop/go/logs/run/
[error]
error_dir = /Users/why/Desktop/go/logs/error/
redis.ini(경로: $GOPATH/config/mysql.ini)
[why]
why_hostname = 127.0.0.1
why_port = 3306
why_db = 0
why_auth = why123
mysql.ini(경로: $GOPATH/config/redis.ini)
[why]
why_hostname = 127.0.0.1
why_username = why
why_password = why123
why_port = 3306
why_db = why
why_charset = utf8
config.go(경로: $GOPATH/src/why/config/.config.go)
go get "github.com/larspensjo/config"필요
package config
import (
"flag"
"fmt"
"github.com/larspensjo/config"
"gopkg.in/ini.v1"
"runtime"
"why/util"
)
type Config struct {
Result map[string]string
Err string
}
const path = "./config/"
func GetConfig(cfgType string, cfgSection string) *ini.Section{
configFile := fmt.Sprintf("%s%s.ini", path, cfgType)
cfgIni, err := ini.Load(configFile)
util.Must(err)
section := cfgIni.Section(cfgSection)
return section
}
func (self *Config) getConfig(conn string, configFile string){
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
cfg, err := config.ReadDefault(configFile) // , Config
if err != nil {
logMsg := fmt.Sprintf("Fail to find %v,%v", configFile, err)
self.Err = logMsg
}
self.Result = map[string]string{}
if cfg.HasSection(conn) { // section( )
options, err := cfg.SectionOptions(conn) // options( )
if err == nil {
for _,v := range options{
optionValue, err := cfg.String(conn, v) // section option
if err == nil {
self.Result[v] = optionValue
}
}
}
}
}
func GetConfigEntrance(cfgType string, cfgSection string) map[string]string {
cfg := new(Config)
cfg.getConfig(cfgSection, path + cfgType + ".ini")
return cfg.Result
}
test.go
package main
import (
"fmt"
"log"
"why/config"
)
func main(){
runLogConfig := make(map[string]string)
runLogSection := "run"
runLogConfig = config.GetConfigEntrance("log", runLogSection)
fmt.Println(runLogConfig)
}
https://ini.unknwon.io/docs/intro/getting_started
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Go Fiber 및 PlanetScale로 REST API 구축 - 4부다시 사용자 핸들러에 UpdateUser라는 새 함수를 추가합니다. 업데이트 사용자를 main.go에 등록 이제 응용 프로그램을 다시 실행하십시오. 이전에 생성한 사용자를 업데이트합니다. 응답 사용자가 존재하지 않을...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.