You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
845 B
32 lines
845 B
package conf
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// 配置文件
|
|
// InitConfig : read configFile to return map config and error info
|
|
func InitConfig(configPath string) (config map[string]string, err error) {
|
|
config = make(map[string]string)
|
|
data, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
return config, err
|
|
}
|
|
lines := strings.Split(string(data), "\n")
|
|
for _, line := range lines {
|
|
// spik empty line , remark line and the line without =
|
|
if strings.TrimSpace(line) == "" || strings.HasPrefix(line, "#") || (strings.Contains(line, "=") == false) {
|
|
continue
|
|
}
|
|
// wipe out remark content
|
|
line = strings.Split(line, "#")[0]
|
|
// get key and value
|
|
item := strings.Split(line, "=")
|
|
if strings.TrimSpace(item[0]) == "" {
|
|
continue
|
|
}
|
|
config[strings.TrimSpace(item[0])] = strings.TrimSpace(item[1])
|
|
}
|
|
return config, nil
|
|
}
|
|
|