(Go) How to use toml files?

前端 未结 5 1110
长发绾君心
长发绾君心 2021-02-15 17:47

As title, I want to know how to use toml files from golang.

Before that, I show my toml examples. Is it right?

[datatitle]
enable = true
userids = [
             


        
5条回答
  •  梦谈多话
    2021-02-15 18:25

    With solution Viper you can use a configuration file in JSON, TOML, YAML, HCL, INI and others properties formats.

    Create file:

    ./config.toml
    

    First import:

    import (config "github.com/spf13/viper")
    

    Initialize:

    config.SetConfigName("config")
    config.AddConfigPath(".")
    err := config.ReadInConfig()
    if err != nil {             
        log.Println("ERROR", err.Error())
    }
    

    And get the value:

    config.GetString("datatitle.12345.prop1")
    config.Get("datatitle.12345.prop1").(int32)
    

    Doc.: https://github.com/spf13/viper

    e.g.: https://repl.it/@DarlanD/Viper-Examples#main.go

提交回复
热议问题