Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
module goutil
|
||||
|
||||
go 1.22.10
|
||||
|
||||
require (
|
||||
github.com/bkaradzic/go-lz4 v1.0.0
|
||||
github.com/elastic/go-elasticsearch/v8 v8.0.0-20210916085751-c2fb55d91ba4
|
||||
github.com/fatih/color v1.15.0
|
||||
github.com/go-sql-driver/mysql v1.5.0
|
||||
github.com/gomodule/redigo v1.8.9
|
||||
github.com/gorilla/websocket v1.4.2
|
||||
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8
|
||||
google.golang.org/grpc v1.45.0
|
||||
google.golang.org/protobuf v1.26.0
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||
golang.org/x/sys v0.6.0 // indirect
|
||||
golang.org/x/text v0.3.6 // indirect
|
||||
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
|
||||
)
|
||||
@@ -0,0 +1,99 @@
|
||||
package configUtil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
// 读取JSON格式的配置文件
|
||||
// config_file_path:配置文件路径
|
||||
// 返回值:
|
||||
// 配置内容的map格式
|
||||
// 错误对象
|
||||
func ReadJsonConfig(config_file_path string) (config map[string]interface{}, err error) {
|
||||
// 读取配置文件(一次性读取整个文件,则使用ioutil)
|
||||
bytes, err := ioutil.ReadFile(config_file_path)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("读取配置文件的内容出错:%s", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 使用json反序列化
|
||||
config = make(map[string]interface{})
|
||||
if err = json.Unmarshal(bytes, &config); err != nil {
|
||||
err = fmt.Errorf("反序列化配置文件的内容出错:%s", err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 从config配置中获取int类型的配置值
|
||||
// config:从config文件中反序列化出来的map对象
|
||||
// configName:配置名称
|
||||
// 返回值:
|
||||
// 配置值
|
||||
// 错误对象
|
||||
func ReadIntJsonValue(config map[string]interface{}, configName string) (value int, err error) {
|
||||
configValue, exist := config[configName]
|
||||
if !exist {
|
||||
err = fmt.Errorf("不存在名为%s的配置或配置为空", configName)
|
||||
return
|
||||
}
|
||||
|
||||
configValue_float, ok := configValue.(float64)
|
||||
if !ok {
|
||||
err = fmt.Errorf("%s必须为int型", configName)
|
||||
return
|
||||
}
|
||||
value = int(configValue_float)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 从config配置中获取string类型的配置值
|
||||
// config:从config文件中反序列化出来的map对象
|
||||
// configName:配置名称
|
||||
// 返回值:
|
||||
// 配置值
|
||||
// 错误对象
|
||||
func ReadStringJsonValue(config map[string]interface{}, configName string) (value string, err error) {
|
||||
configValue, exist := config[configName]
|
||||
if !exist {
|
||||
err = fmt.Errorf("不存在名为%s的配置或配置为空", configName)
|
||||
return
|
||||
}
|
||||
|
||||
configValue_string, ok := configValue.(string)
|
||||
if !ok {
|
||||
err = fmt.Errorf("%s必须为string型", configName)
|
||||
return
|
||||
}
|
||||
value = configValue_string
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 从config配置中获取string类型的配置值
|
||||
// config:从config文件中反序列化出来的map对象
|
||||
// configName:配置名称
|
||||
// 返回值:
|
||||
// 配置值
|
||||
// 错误对象
|
||||
func ReadBoolJsonValue(config map[string]interface{}, configName string) (value bool, err error) {
|
||||
configValue, exist := config[configName]
|
||||
if !exist {
|
||||
err = fmt.Errorf("不存在名为%s的配置或配置为空", configName)
|
||||
return
|
||||
}
|
||||
|
||||
configValue_bool, ok := configValue.(bool)
|
||||
if !ok {
|
||||
err = fmt.Errorf("%s必须为bool型", configName)
|
||||
return
|
||||
}
|
||||
value = configValue_bool
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user