package config
type Redis struct {
Host string `yaml:"host"`
Password string `yaml:"password"`
Port int `yaml:"port"`
DB int `yaml:"db"`
}
package config
type Logger struct {
LogPath string `yaml:"logPath"`
Debug bool `yaml:"debug"`
}
package config
type Config struct {
Redis Redis `yaml:"redis"`
Logger Logger `yaml:"logger"`
}
package global
import "test/config"
var (
Cfg config.Config
)
package initialization
import (
"test/global"
"gopkg.in/yaml.v2"
"log"
"os"
)
func initConfig() {
configFile, err := os.ReadFile("config.yaml")
if err != nil {
log.Fatal("读取配置文件错误!", err)
}
if err = yaml.Unmarshal(configFile, &global.Cfg); err != nil {
log.Fatal("加载配置文件错误!", err)
}
}
redis:
host: "127.0.0.1"
port: 6379
password: ""
db: 0
logger:
logPath: "./log/test.log"
debug: true