无法解码 TOML 文件

huangapple go评论126阅读模式
英文:

Unable to decode toml file

问题

我想从一个toml文件中读取配置。

conf/conf.toml

  1. db_host = "127.0.0.1"
  2. db_port = 3306
  3. db_user = "root"
  4. db_password ="123456"

conf/conf.go文件

  1. package conf
  2. import (
  3. "log"
  4. "github.com/BurntSushi/toml"
  5. )
  6. type appcfg struct {
  7. DbHost string `toml:"db_host"`
  8. DbPort string `toml:"db_port"`
  9. DbUser string `toml:"db_user"`
  10. DbPassword string `toml:"db_password"`
  11. }
  12. var (
  13. App *appcfg
  14. defConfig = "./conf/conf.toml"
  15. )
  16. func init() {
  17. var err error
  18. App, err = initCfg()
  19. log.Println(App.DbHost)
  20. }
  21. func initCfg() (*appcfg, error) {
  22. app := &appcfg{}
  23. _, err := toml.DecodeFile(defConfig, &app)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return app, nil
  28. }

当我运行这个程序时,我得到一个我不知道如何修复的错误:

panic: runtime error: invalid memory address or nil pointer dereference

英文:

I want to read configs from a toml file.

conf/conf.toml

  1. db_host = "127.0.0.1"
  2. db_port = 3306
  3. db_user = "root"
  4. db_password ="123456"

conf/conf.go file

  1. package conf
  2. import (
  3. "log"
  4. "github.com/BurntSushi/toml"
  5. )
  6. type appcfg struct {
  7. DbHost string `toml:"db_host"`
  8. DbPort string `toml:"db_port"`
  9. DbUser string `toml:"db_user"`
  10. DbPassword string `toml:"db_password"`
  11. }
  12. var (
  13. App *appcfg
  14. defConfig = "./conf/conf.toml"
  15. )
  16. func init() {
  17. var err error
  18. App, err = initCfg()
  19. log.Println(App.DbHost)
  20. }
  21. func initCfg() (*appcfg, error) {
  22. app := &appcfg{}
  23. _, err := toml.DecodeFile(defConfig, &app)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return app, nil
  28. }

When I run this program, I get an error that I don't know how to fix:

> panic: runtime error: invalid memory address or nil pointer dereference

答案1

得分: 0

(重新发布Comin2021的现已删除的答案,因为它被提问者接受了)

你将DbPort的类型定义为string,但在你的配置文件中它显示为整数。请按以下方式进行更改:

  1. type appcfg struct {
  2. DbHost string `toml:"db_host"`
  3. DbPort int64 `toml:"db_port"` // 将此处更改
  4. DbUser string `toml:"db_user"`
  5. DbPassword string `toml:"db_password"`
  6. }

还要检查initCfg的第二个返回值err是否为空,并将其记录下来。

英文:

(Reposting Comin2021's now deleted answer in English, since it was accepted by the OP)

You defined the type of your DbPort as string but it appears as an integer in your configuration file. Change it as below:

  1. type appcfg struct {
  2. DbHost string `toml:"db_host"`
  3. DbPort int64 `toml:"db_port"` // change this
  4. DbUser string `toml:"db_user"`
  5. DbPassword string `toml:"db_password"`
  6. }

Also check that initCfg second return value err is not empty and log it.

huangapple
  • 本文由 发表于 2022年5月24日 14:17:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/72358006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定