无法在config golang节中存储数据

i just try to make config with ini file for my first app.

i have config file like this

[server]
port         = 1020
environment  = "development"

and some code to read the config file.

package config

import (
    "log"

    "gopkg.in/gcfg.v1"
)

type ServerConfig struct {
    Port        int    `gcfg:"port"`
    Environment string `gcfg:"environment"`
}

var Server ServerConfig

func InitConfig() {
    err := InitConfigServer()
    if err != nil {
        log.Panicln("Error init config server : ", err)
        return
    }
}

func InitConfigServer() error {
    err := gcfg.ReadFileInto(&Server, "files/config/config-main.ini")
    return err
}

and my main.go will call InitConfig() when i try to run main.go, i got error

Error init config server :  warnings:
can't store data at section "server"
can't store data at section "server"

panic: Error init config server :  warnings:
can't store data at section "server"
can't store data at section "server"

can someone explain what actually happened with that errors? i can't clearly understand what wrong with this configuration file.

Thanks

Per the documentation: "section corresponds to a struct field in the config struct". You should be reading into a struct with a field Server (to take the server section). That field could be of type ServerConfig:

type Config struct {
    Server ServerConfig `gcfg:"server"`
}

type ServerConfig struct {
    Port        int    `gcfg:"port"`
    Environment string `gcfg:"environment"`
}

var Config Config

func InitConfig() {
    err := InitConfigServer()
    if err != nil {
        log.Panicln("Error init config server : ", err)
        return
    }
}

func InitConfigServer() error {
    err := gcfg.ReadFileInto(&Config, "files/config/config-main.ini")
    return err
}