新增配置模块
This commit is contained in:
3
config.yaml
Normal file
3
config.yaml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
port: 8989
|
||||||
|
#服务名称
|
||||||
|
service_name: "pmg"
|
||||||
28
config/index.go
Normal file
28
config/index.go
Normal 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user