From e6f0cf3f469e27a6cbfdadec082c7282e1192fa8 Mon Sep 17 00:00:00 2001 From: zyj <18107291228@163.com> Date: Mon, 14 Jul 2025 16:44:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=85=8D=E7=BD=AE=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 3 +++ config/index.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 config.yaml create mode 100644 config/index.go 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 +}