Skip to content

Instantly share code, notes, and snippets.

@backerman
Created December 7, 2015 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save backerman/6c75b18c90f132d818bf to your computer and use it in GitHub Desktop.
Save backerman/6c75b18c90f132d818bf to your computer and use it in GitHub Desktop.
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