configor icon indicating copy to clipboard operation
configor copied to clipboard

yaml的值无法映射到结构体上

Open junixapp opened this issue 5 months ago • 1 comments

go.mod

go 1.23.6

require github.com/jinzhu/configor v1.2.2

require (
	github.com/BurntSushi/toml v1.2.0 // indirect
	gopkg.in/yaml.v3 v3.0.1 // indirect
)

config目录是config.go和config.yaml, config.yaml文件如下:

HttpPort: 8080
DbUrl: test

config.go内容如下:

package config

import (
	"encoding/json"
	"github.com/jinzhu/configor"
	"log"
)

type Config struct {
	HttpPort string
	DbUrl    string
}

var Conf Config

func Init() {
	//生产环境需添加  CONFIGOR_ENV=production go run main.go
	err := configor.Load(&Conf, "config/config.yaml")
	if err != nil {
		log.Fatalf("配置加载失败 %v", err)
		return
	}
	js, _ := json.MarshalIndent(&Conf, "", "  ")
	log.Println("当前环境:", configor.ENV(), "\n", string(js))
}

改为下划线http_port也无法获取到值,运行输出如下

2025/11/19 10:25:20 当前环境: development 
 {
  "HttpPort": "",
  "DbUrl": ""
}

junixapp avatar Nov 19 '25 02:11 junixapp

试了好久发现yaml要这样写:

httpport: 8080
dburl: xxxx

Java转过来的怎么都想不到是这么个写法,默认要不是驼峰转下划线,要么是同名匹配,怎么也想不到是转小写,救救孩子吧!!!!!!

junixapp avatar Nov 19 '25 02:11 junixapp