Go-如何读取yaml,json,ini等配置文件详解编程语言
2023-06-13 09:11:48 时间
1. json使用
type conf struct { Enabled bool `yaml:"enabled"` //yaml:yaml格式 enabled:属性的为enabled Path string `yaml:"path"` func (c *conf) getConf() *conf { yamlFile, err := ioutil.ReadFile("conf.yaml") if err != nil { log.Printf("yamlFile.Get err #%v ", err) err = yaml.Unmarshal(yamlFile, c) if err != nil { log.Fatalf("Unmarshal: %v", err) return c func main() { var c conf c.getConf() fmt.Println("path:" + c.Path)
JSON 应该比较熟悉,它是一种轻量级的数据交换格式。层次结构简洁清晰 ,易于阅读和编写,同时也易于机器解析和生成。
创建 conf.json:{ "enabled": true, "path": "/usr/local"新建config_json.go:
package main import ( "encoding/json" "fmt" "os" type configuration struct { Enabled bool Path string func main() { // 打开文件 file, _ := os.Open("conf.json") // 关闭文件 defer file.Close() //NewDecoder创建一个从file读取并解码json对象的*Decoder,解码器有自己的缓冲,并可能超前读取部分json数据。 decoder := json.NewDecoder(file) conf := configuration{} //Decode从输入流读取下一个json编码值并保存在v指向的值里 err := decoder.Decode( conf) if err != nil { fmt.Println("Error:", err) fmt.Println("path:" + conf.Path)
启动运行后,输出如下:
D:/Go_Path/go/src/configmgr go run config_json.go path:/usr/local2. ini的使用
INI文件格式是某些平台或软件上的配置文件的非正式标准,由节(section)和键(key)构成,比较常用于微软Windows操作系统中。这种配置文件的文件扩展名为INI。
创建 conf.ini:[Section] enabled = true path = /usr/local # another comment
2.下载第三方库:go get gopkg.in/gcfg.v1
新建 config_ini.go:package main import ( "fmt" gcfg "gopkg.in/gcfg.v1" func main() { config := struct { Section struct { Enabled bool Path string }{} err := gcfg.ReadFileInto( config, "conf.ini") if err != nil { fmt.Println("Failed to parse config file: %s", err) fmt.Println(config.Section.Enabled) fmt.Println(config.Section.Path)
启动运行后,输出如下:
D:/Go_Path/go/src/configmgr go run config_ini.go true /usr/local3. yaml使用
yaml 可能比较陌生一点,但是最近却越来越流行。也就是一种标记语言。层次结构也特别简洁清晰 ,易于阅读和编写,同时也易于机器解析和生成。
golang的标准库中暂时没有给我们提供操作yaml的标准库,但是github上有很多优秀的第三方库开源给我们使用。
创建 conf.yaml:enabled: true path: /usr/local
type conf struct { Enabled bool `yaml:"enabled"` //yaml:yaml格式 enabled:属性的为enabled Path string `yaml:"path"` func (c *conf) getConf() *conf { yamlFile, err := ioutil.ReadFile("conf.yaml") if err != nil { log.Printf("yamlFile.Get err #%v ", err) err = yaml.Unmarshal(yamlFile, c) if err != nil { log.Fatalf("Unmarshal: %v", err) return c func main() { var c conf c.getConf() fmt.Println("path:" + c.Path)
启动运行后,输出如下:
D:/Go_Path/go/src/configmgr go run config_yaml.go path:/usr/local
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/18135.html
cgojavawindows相关文章
- 20.Go语言-函数是一等公民
- Go 编程 | 连载 29 - Go 与 JSON
- 造一轮子:vscode插件--支持json生成go struct,curl生成go代码
- 3分钟带你了解 Go 密码学编程之 Hex 编码!
- 【Golang报错】100% 解决 json: cannot unmarshal array into Go value of type model.xxx 问题
- 2022-12-31:以下go语言代码输出什么?A:1 1;B:-1 1;C:-1 -1;D:编译错误。 package main import “fmt“ f
- R语言多元(多变量)GARCH :GO-GARCH、BEKK、DCC-GARCH和CCC-GARCH模型和可视化|附代码数据
- Go读取yaml文件到struct类
- go手动添加锁=安全
- 泛型已经被迅速采用,错误处理是新的大挑战|Go 开发者调查报告
- 2023-03-30:用Go语言改写FFmpeg示例decode_audio.c,实现高效音频解码。
- GO数据结构(一)——稀疏数组
- Go语言——Json处理
- Go-包管理-go get(一)
- go fmt命令——格式化代码文件
- Go从入门到精通(一)go语言初识详解编程语言
- Go_ go mod 命令解决墙的问题详解编程语言
- go语言之range详解编程语言
- MySQL支持JSON格式:优势何在?(mysql的json格式)
- 字段MySQL查询获取JSON字段详解(mysql取json)
- 如何用 GVM 管理 Go 项目
- 精简操作以JSON方式修改Oracle数据库中的数据(json修改oracle)
- 提升Go读取Oracle的速度(go读取oracle速度)
- 踏上oracle编程之路,Go(go oracle编程)
- Go语言interface详解