2

I have a problem using a viper, I have assigned variable by viper method, so when I try to get value inside any function I have a null value. Does anybody have any idea why does it happen so? Any other variables initialization works fine, but not viper GetString method.

Structure: main.go

    package main

    import (
        "project/Model"
        "github.com/spf13/viper"
        ...
    )

    func main() {
        //Config handling
        viper.SetConfigName("main")
        viper.AddConfigPath("/config/")
        err = viper.ReadInConfig()
        ...
    }

Package Model

    package Model

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

    var sqlhost = viper.GetString("db.host")

    func foo() {
        log.Println(sqlhost)
    }    
stevenferrer
  • 2,504
  • 1
  • 22
  • 33
  • Since `viper.GetString` return `string` (not a pointer), it won't be null. Do you mean that the return value is *empty string*? First, check whether the key exists or not by `viper.IsSet`. – putu Oct 04 '17 at 06:39
  • sorry, yes, string is empty. Key exists, because when I'm using getString inside func it gives me a value which i need. – Илья Покровский Oct 04 '17 at 06:55
  • 1
    It means that `viper.GetString` is called before viper configuration is initialized, e.g. by `viper.ReadConfig` etc. Use [`init()`](https://stackoverflow.com/questions/24790175/when-is-the-init-function-run) function. – putu Oct 04 '17 at 07:02
  • Probably more of a style difference, but I'd prefer the `viper.ReadConfig` etc. to be in the `main()` function, and retrieving the values inside of the functions that use it rather than as a package var initialization. – John Weldon Oct 04 '17 at 07:20
  • well I follow Your advice, it works now. Thank You so much for Your help! – Илья Покровский Oct 04 '17 at 08:53

1 Answers1

0

I suspect that doing package variable initialization as you show will lead to the viper configuration not being complete before performing the GetString(), and therefore the zero (empty) string value is what is returned. Without seeing how you initialize your viper config, I'm guessing that it's not in an init() function, but is in a main() function that hasn't run yet when the package variable is assigned.

You should probably retrieve the viper.GetString("db.host") inside the highest level function that needs it rather than during package initialization.

John Weldon
  • 39,849
  • 11
  • 94
  • 127
  • well viper initialization is in a main() function You're guessing right (and excuse me, i missed it), but variable init is in another package – Илья Покровский Oct 04 '17 at 07:08
  • Yeah, see https://golang.org/ref/spec#Package_initialization and https://golang.org/doc/effective_go.html#initialization. Probably the other package is initializing viper after this variable is assigned – John Weldon Oct 04 '17 at 07:12
  • well, i edited my question a bit, i cannot realize why the method inside the function returns right value, but the outside call gives empty. if it would be reinitialization it wouldn't give me a config value. – Илья Покровский Oct 04 '17 at 07:41