新增配置模块

This commit is contained in:
zyj
2025-07-14 16:44:42 +08:00
parent e159cf5b1c
commit e6f0cf3f46
2 changed files with 31 additions and 0 deletions

28
config/index.go Normal file
View File

@@ -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
}