【go语言开发】yaml文件配置和解析

小咪咪 2024-05-24 05:40 195阅读 0赞

本文主要介绍使用第三方库来对yaml文件配置和解析。首先安装yaml依赖库;然后yaml文件中配置各项值,并给出demo参考;最后解析yaml文件,由于yaml文件的配置在全局中可能需要,可定义全局变量Config,便于调用

文章目录

  • 安装yaml依赖库
  • 配置yaml文件
  • 解析yaml文件

欢迎大家访问个人博客网址:https://www.maogeshuo.com,博主努力更新中…

安装yaml依赖库

命令行执行如下命令:

  1. go get gopkg.in/yaml.v3

配置yaml文件

config-dev.yaml 文件中配置所需key-value,参考如下文件配置

  1. app:
  2. name: code-go
  3. version: v0.0.1
  4. introduction: "code-go是一个学习go的项目~"
  5. server:
  6. host: localhost
  7. port: 8080
  8. appMode: debug # debug 开发模式 release 生产模式
  9. database:
  10. mysql:
  11. # 用户名
  12. username: root
  13. # 密码
  14. password: 123456
  15. # 数据库名
  16. database: code-go
  17. # 主机地址
  18. host: localhost
  19. # 端口
  20. port: 3306
  21. # 是否开启日志
  22. log-mode: true
  23. redis:
  24. host: localhost
  25. port: 6379
  26. password: ""
  27. db: 0 #数据库编号
  28. log:
  29. # 通道数据大小
  30. chan-size: 30
  31. # 每三条保存数据一次
  32. save-log-num: 3

解析yaml文件

  1. package core
  2. import (
  3. "fmt"
  4. "gopkg.in/yaml.v3"
  5. "os"
  6. )
  7. var Config YamlConfig
  8. type YamlConfig struct {
  9. App App `yaml:"app"`
  10. Server ServerConfig `yaml:"server"`
  11. Database DatabaseConfig `yaml:"database"`
  12. Redis RedisConfig `yaml:"redis"`
  13. Log LogConfig `yaml:"log"`
  14. }
  15. type App struct {
  16. Name string `yaml:"name"`
  17. Version string `yaml:"version"`
  18. Introduction string `yaml:"introduction"`
  19. }
  20. type ServerConfig struct {
  21. Host string `yaml:"host"`
  22. Port int `yaml:"port"`
  23. AppMode string `yaml:"appMode"`
  24. }
  25. type DatabaseConfig struct {
  26. Mysql MysqlConfig `yaml:"mysql"`
  27. }
  28. type MysqlConfig struct {
  29. UserName string `yaml:"username"`
  30. Password string `yaml:"password"`
  31. Database string `yaml:"database"`
  32. Host string `yaml:"host"`
  33. Port string `yaml:"port"`
  34. LogMode bool `yaml:"log-mode"`
  35. }
  36. type RedisConfig struct {
  37. Host string `yaml:"host"`
  38. Port int `yaml:"port"`
  39. Password string `yaml:"password"`
  40. Db int `yaml:"db"`
  41. }
  42. type LogConfig struct {
  43. ChanSize int `yaml:"chan-size"`
  44. SaveLogNum int `yaml:"save-log-num"`
  45. }
  46. // InitConfigDev 初始化yaml config
  47. func InitConfigDev() error {
  48. file, err := os.ReadFile("./config-dev.yaml")
  49. if err != nil {
  50. LOG.Println("read config-dev.yaml fail", err)
  51. return err
  52. }
  53. err = yaml.Unmarshal(file, &Config)
  54. if err != nil {
  55. LOG.Println("yaml unmarshal fail")
  56. return err
  57. }
  58. return nil
  59. }
  60. // PrintConfig 打印配置
  61. func PrintConfig() {
  62. fmt.Println("**************************************************")
  63. fmt.Println("* ")
  64. fmt.Println("* Welcome to code-go ")
  65. fmt.Printf("* Introduction: %s\n", Config.App.Introduction)
  66. fmt.Printf("* Environment: %s\n", Config.Server.AppMode)
  67. fmt.Printf("* Click url:http://localhost:%d\n", Config.Server.Port)
  68. fmt.Printf("* Swagger url: http://localhost:%d/swagger/index.html\n", Config.Server.Port)
  69. fmt.Println("* ")
  70. fmt.Println("**************************************************")
  71. }

定义了一个YamlConfig结构体,并在结构体中嵌套了App、ServerConfig等结构体,以对应YAML文件中的不同层级。

通过使用.ReadFile函数读取YAML文件的内容,然后使用yaml.Unmarshal函数将文件内容解析为结构体对象。在Unmarshal函数的第二个参数中,传入了指向config结构体的指针,以便将解析后的内容赋值给结构体。

最后,我们可以通过PrintConfig访问结构体的字段来获取解析后的配置信息,并进行相应的操作。

注意:
博主将config-dev.yaml 放在项目根目录下,因此调用os.ReadFile 配置的./config-dev.yaml

终端打印:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,195人围观)

还没有评论,来说两句吧...

相关阅读