'Unable to decode toml file

I want to read configs from a toml file.

conf/conf.toml

db_host = "127.0.0.1"

db_port = 3306

db_user = "root"

db_password ="123456"

conf/conf.go file

package conf

import (
    "log"
    "github.com/BurntSushi/toml"
)

type appcfg struct {
    DbHost     string `toml:"db_host"`
    DbPort     string `toml:"db_port"`
    DbUser     string `toml:"db_user"`
    DbPassword string `toml:"db_password"`
}

var (
    App       *appcfg
    defConfig = "./conf/conf.toml"
)

func init() {

    var err error
    App, err = initCfg()
    log.Println(App.DbHost)

}

func initCfg() (*appcfg, error) {
    app := &appcfg{}
    _, err := toml.DecodeFile(defConfig, &app)
    if err != nil {
        return nil, err
    }
    return app, nil
}

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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source