diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..70e900a --- /dev/null +++ b/config.yaml @@ -0,0 +1,3 @@ +port: 8989 +#服务名称 +service_name: "pmg" \ No newline at end of file diff --git a/config/index.go b/config/index.go new file mode 100644 index 0000000..4a7ab01 --- /dev/null +++ b/config/index.go @@ -0,0 +1,28 @@ +package config + +import ( + "os" + + "gopkg.in/yaml.v3" +) + +type AppConfig struct { + Port int `yaml:"port"` + ServiceName string `yaml:"service_name"` +} + +func LoadConfig() (*AppConfig, error) { + // 读取文件内容 + data, err := os.ReadFile("config.yaml") + if err != nil { + return nil, err + } + + // 解析 YAML + var cfg AppConfig + if err := yaml.Unmarshal(data, &cfg); err != nil { + return nil, err + } + + return &cfg, nil +}