Preflight Checklist

  • [X] I have searched the issue tracker for an issue that matches the one I want to file, without success.

Problem Description

Currently, Viper only supports retrieving values from environment variables and it requires the user to specify the exact environment variable name in the struct tag. It would be useful to have the ability to specify a default value in the struct tag as well, so that if the environment variable is not set, the default value will be used instead.

Proposed Solution

The proposed solution is to add a new field in the struct tag called "default" which will hold the default value. The syntax for the struct tag will be as follows:

`env:"ENV_VAR_NAME,default=DEFAULT_VALUE"`

For example:

type Config struct {
    Port int `env:"PORT,default=8080"`
}
````

### Alternatives Considered

_No response_

### Additional Information

_No response_

**Comment From: github-actions[bot]**

👋 Thanks for reporting!

A maintainer will take a look at your issue shortly. 👀

In the meantime: We are working on **Viper v2** and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues.

⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9

📣 If you've already given us your feedback, you can still help by spreading the news,
either by sharing the above link or telling people about this on Twitter:

https://twitter.com/sagikazarmark/status/1306904078967074816

**Thank you!** ❤️


**Comment From: tkrop**

I love this proposal, and I definitely would like some support for setting default values using tags, however reusing the `env`-tag seems to create redundant information that makes the name of environment variables unclear. So using something simpler as [go-defaults](https://github.com/mcuadros/go-defaults) `default`-tag during initial creation of the root struct would be more sufficient.

This would result in support for

type Config struct { Port int default=8080" } ```

However, since go-defaults can actually be applied to the input struct before being inserted, it actually turns out that the core issue is to define the environment variable names for default in viper. So one would actually just need to extract a key value list of the base struct to setup defaults.

Viper support for such a feature could therefor just consist of allowing to register a default configuration object that is providing all basic default values. In the end it than does not matter whether this template is provided by go-defaults or any other way to setup a default structure.

Comment From: Amirmahdikahdouii

I have this issue too! Is there any implementation in viper for this? because I haven't seen in documents!

Comment From: arvindh123

I have this issue too! Is there any implementation in viper for this? because I haven't seen in documents!

https://github.com/caarlos0/env this library may be helpful, if config values are passed only via environment variable

Comment From: github-actions[bot]

Issues with no activity for 30 days are marked stale and subject to being closed.