网站的建设运营收费是哪些,深圳市官方网站开发公司,上海专业建站公,网站建设与开发课程介绍下列代码#xff0c;设计 Config 和 MyConfig 是为可扩展 Config#xff0c;同时 Config 作为公共部分可保持变化。采用了匿名的内嵌结构体#xff0c;但又不希望 yaml 结果多出一层。如果 MyConfig 中的 Config 没有使用“yaml:,inline”修饰#xff0c;则读取…下列代码设计 Config 和 MyConfig 是为可扩展 Config同时 Config 作为公共部分可保持变化。采用了匿名的内嵌结构体但又不希望 yaml 结果多出一层。如果 MyConfig 中的 Config 没有使用“yaml:,inline”修饰则读取不到配置Config 中的 Db 将为 nil。
package mainimport (fmtosgopkg.in/yaml.v2
)type DbConfig struct {Dsn string yaml:dsn
}type Config struct {Db *DbConfig yaml:db
}type MyConfig struct {Config yaml:,inline
}/*
上述 MyConfig 对应的 yaml 文件db:dsn: dsn123
*/func main() {bytes, err : os.ReadFile(./test.yaml)if err ! nil {panic(err)}config : MyConfig{}err yaml.Unmarshal(bytes, config)if err ! nil {panic(err)}fmt.Printf(%v\n, *config)
}