Last active
August 29, 2015 14:08
-
-
Save ricston-git/8c9d26da4592e2856936 to your computer and use it in GitHub Desktop.
Using Viper for some Go basics
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
$ mkdir useviper_workspace | |
$ cd useviper_workspace | |
useviper_workspace$ mkdir src bin pkg |
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
useviper_workspace$ go get | |
can’t load package: package .: no buildable Go source files in /Users/justin/go-stuff/useviper_workspace |
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
#!/bin/bash | |
if [ $# -eq 0 ] | |
then | |
echo "go install github.com/justincalleja/useviper" && go install github.com/justincalleja/useviper | |
else | |
echo "go install github.com/justincalleja/$1" && go install github.com/justincalleja/$1 | |
fi |
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
useviper_workspace$ goinstall.sh | |
useviper_workspace$ goinstall.sh useviper | |
useviper$ go install |
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
useviper_workspace$ mkdir -p src/github.com/justincalleja/useviper |
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
func main() { | |
viper.SetConfigName("config") // name of config file (without extension) | |
viper.AddConfigPath(".") // path to look for the config file in | |
err := viper.ReadInConfig() | |
if err != nil { | |
fmt.Println("Config not found...") | |
} else { | |
name := viper.GetString("name") | |
fmt.Println("Config found, name = ", name) | |
} | |
} |
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
useviper_workspace$ echo $GOPATH | |
/Users/justin/go-stuff/useviper_workspace | |
useviper_workspace$ type set_gopath | |
set_gopath is a function | |
set_gopath () | |
{ | |
export GOPATH=pwd; | |
export PATH=$GOPATH/bin:$PATH | |
} |
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
useviper_workspace$ tree -L 3 | |
. | |
├── bin | |
│ └── useviper | |
├── pkg | |
│ └── darwin_amd64 | |
│ ├── github.com | |
│ └── gopkg.in | |
└── src | |
├── github.com | |
│ ├── BurntSushi | |
│ ├── justincalleja | |
│ ├── kr | |
│ ├── mitchellh | |
│ └── spf13 | |
└── gopkg.in | |
└── yaml.v1 |
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/viper" | |
) | |
func main() { | |
fmt.Println("in main") | |
viper.SetConfigName("config") // name of config file (without extension) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment