Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
    Till Wegmüller
    AFAIK yes. Curious idea to watch for environment variables for changes. It's the first time I have heard of it. How would change them anyhow. Is there a Way in C to do so?
    or Bash?
    Vadim Yermolin
    Welp, I'm not sure this is possible either...
    Hi all
    can someone please review and approve #450 since it should fix a game-breaking problem?
    Hi, just meeting viper, i have problem that it won't read from file each time when viper.Get("key"), right? (i hope it was loaded in memory
    Noah Gray
    excuse the newb question, but how would i use "or" here
    cfg := &ServerConfig{
      RedisURL:                viper.GetString("redis_port_6379_tcp"),
    would this do
    cfg := &ServerConfig{
      RedisURL:                viper.GetString("redis_port_6379_tcp") | viper.GetString("database_url"),
    var redisURL = viper.GetString("redis_port_6379_tcp")
    if viper.GetString("database_url") != "" {
          redisURL= viper.GetString("database_url"),
    cfg := &ServerConfig{
      RedisURL:                redisURL,
    Stephen Hoekstra

    Hi! I've noticed when loading an HCL file that it is read with slices under each key and not as a hash. The file is read correctly when using hcl.Decode, so it seems to be something with viper.

    I seem to hit the same thing whether using viper.ReadInConfig or viper.ReadConfig and wondering something had come across this or if I'm doing something wrong. (not sure best way to share example code and output)

    Shivaprasad Bhat
    Hello All!. Need help. I am using viper for one of my projects. There are different environments - dev, testing, CI etc.. the config list is very large to override values through environment variables. so my question is what would be the best way to have a base config and override as required for different environments?..
    expecting something like
    db_host: localhost
    db_user: appuser
        db_host: test-host
    how do I pass string arrays to viper/cobra from command line ?
    Joe Percivall

    Hello Devs, I use Viper to handle the configuration for a REST service. DELETE/POST/PUT requests have the potential to modify the config after which I use viper.WriteConfig() to persist the changes.

    My question is whether other or not there's a built-in or easy way to do a rolling history of the last X number of configs? I've looked at the docs, and I don't believe so, but wanted to reach out to double check.

    Márk Sági-Kazár
    I don't think there is, but I hardly think it should be viper's responsibility
    Alessandro Tagliapietra
    Hello everyone, is there a way to parse something like this?
        - frequency: "@daily"
          minVal: 1
          maxVal: 1000000
    Alessandro Tagliapietra
    ohh, unmarshal :D
    Akaash Patnaik
    Hi all. I'm running into a problem trying to pass my Viper object around my app (a REST API) as a singleton. When using it within the app, I initialise the Viper object in the main package, from a config yaml and pass this same object around and things work great. But when trying to initialise the config from test files in different packages the yaml isn't accessible because Viper tries to find it relative to the package it's being initialised from.
    I guess I must be doing this wrong. Any suggestions? Or can anyone point me to any reference code where the test code in a different package accesses the same config as the app?
    Akaash Patnaik
    I should mention that the config yaml currently lives alongside some initialisation code within a "config" package under my base package
    Akaash Patnaik
    For anyone who's interested, I've just decided to work my way around the problem by passing the config file location as an argument to the function that initialises the Viper object. Not sure if this is an anti-pattern, but it fixes my issues for now.
    Márk Sági-Kazár
    I don't think that's an anti-pattern.
    Justus Luthy (Boatright)
    confirmation: assuming that I have an environment variable MY_VARIABLE, does Viper do any kind of default rewriting (aside from lowercase) to access that value? So Get("my_variable")...? If I want it to be camelCase I'll need to SetEnvKeyReplacer, right? (Looking at viper_test.go it seems this is how it works).
    haha... just noticed the last message in here was 4 months ago. :)
    How to read json/yaml/properties files from GITURI?
    Chris Wright
    Hi, I've only recently started learning Go but have been trying to use it as much as possible at work. Naturally config and cli arg parsing comes up a lot so naturally came across Viper. I then encountered an issue (spf13/viper#324) with Unmarshal in that it doesn't support keys with dots (eg. having a dictionary key be a domain name or in my case a Docker tag). I'm having a look at the source to see if I can find a proper way to fix this but have also noticed the backlog of issues and pull requests. I'd like to get more into open source contribution and would love the opportunity to help you guys out in any way I can regarding project maintenance if you might consider it? I'm a full time developer at work, currently focusing on devops and sysops. Let me know if you'd be interested or want any more info, cheers!
    @spf13 Are there any plans to merge spf13/viper#543 soon?
    Alexandr Savvinov
    Daniel Carlier
    Pedro Silva
    Hello everyone. Can anyone contribute to spf13/viper? Don't see a contributing.md and am just wondering if anyone will actually look at PRs and merge them
    Pedro Silva
    Is this project still maintained? :S
    Márk Sági-Kazár
    Hi Pedro! Yes, anyone can contribute. Yes, the project is maintained, but it's an open source project, so efforts spent on it is limited
    Márk Sági-Kazár
    @madiganz I merged the linked PR.
    @sagikazarmark Thank you!

    Hey guys. I am not sure this is the correct place for this but....

    I am using Viper to manage environment variables for a web app. It works perfectly if I run in normally....

    So the web app reads a couple of values from the environment using AutomaticEnv() and a couple of calls to GetString().

    If I package this up into a docker container viper will not be able to grab the env values it would if running outside the docker container. I know the environment variables are getting set. I can exec into the container see it.

    I tried switching to os.Getenv(). That call is able to pull in the values.

    Any ideas what's broken?

    Rotem Erlich
    I made PR #760 for read only view using separated interface. It also have support for sub config with environment variables support (fix #643)
    Pedro Silva
    @sagikazarmark it just seems to take for ever to get things merged. Maybe the people responsible for the repo should find others to help maintain the repo so it doesn't take 3 months to get PRs merged
    Just NON
    hello i have a problem when i use viper.GetString("") outside main package return nil somebody help

    hello i have a problem when i use viper.GetString("") outside main package return nil somebody help
    `package main

    import (

    func main() {
    //Config handling
    err = viper.ReadInConfig()

    `package Model

    import ("github.com/spf13/viper"

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

    func foo() {
    } `

    Just NON
    someone help
    Márk Sági-Kazár
    @ivybridge-3c33 I answered to your issue. TL;DR since you read in the config in main and the variable is evaluated before main is executed, this won't work. You have to fetch data from Viper after you read in the config file.
    Just NON
    Thank you
    Tobias Frischholz
    Hello guys,
    Maybe I found a bug, but I'am not sure...

    I just want to write a config with mapstructure annotations on it...

        type Person struct {
            Age    int    `mapstructure:"a"`
            Gender string `mapstructure:"g"`
            Family string `mapstructure:"f"`
            Name   string `mapstructure:"n"`

    The write method to write a config file:

    import (
    func WriteConfig(configInstance interface{}, filePath string,) error {
        v := viper.New()
        b, err := yaml.Marshal(&configInstance)
        if err != nil {
            return err
        defaultConfig := bytes.NewReader(b)
        if err := v.MergeConfig(defaultConfig); err != nil {
            return err
        return v.WriteConfigAs(filePath)

    The result for yaml or json is always:

      "age": 48,
      "family": "Family-Name",
      "gender": "m",
      "name": "Name"


    age: 48
    family: Family-Name
    gender: m
    name: Name

    The mapstructure annotation are not respected..
    Do I make something wrong here???

    btw. I am using the latest version from viper 1.6.1
    Márk Sági-Kazár
    yaml.Marshal won't respect mapstructure tag. Either use yaml tag in mapstructure as well (you can do that by configuring mapstructure), or first marshal your config to a map using mapstructure and then marshal that to yaml.