Created
December 7, 2015 01:47
-
-
Save backerman/6c75b18c90f132d818bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/spf13/pflag" | |
"github.com/spf13/viper" | |
) | |
type config struct { | |
Foo bool | |
} | |
var ( | |
c config | |
) | |
func main() { | |
flag := pflag.Bool("Foo", false, "Test flag.") | |
viper.BindPFlag("Foo", pflag.Lookup("Foo")) | |
pflag.Parse() | |
// Unmarshal without an explicit viper default | |
viper.Unmarshal(&c) | |
lookedup := viper.GetBool("Foo") | |
fmt.Printf("foo unmarshals as: %v\n", c.Foo) | |
fmt.Printf("foo is looked up as: %v\n", lookedup) | |
fmt.Printf("foo is actually: %v\n", *flag) | |
// Do it again with the redundant default | |
viper.SetDefault("Foo", false) | |
viper.Unmarshal(&c) | |
fmt.Printf("foo now unmarshals as: %v\n", c.Foo) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment