Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Viper for some Go basics
$ mkdir useviper_workspace
$ cd useviper_workspace
useviper_workspace$ mkdir src bin pkg
useviper_workspace$ go get
can’t load package: package .: no buildable Go source files in /Users/justin/go-stuff/useviper_workspace
#!/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
useviper_workspace$ goinstall.sh
useviper_workspace$ goinstall.sh useviper
useviper$ go install
useviper_workspace$ mkdir -p src/github.com/justincalleja/useviper
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)
}
}
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
}
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
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
You can’t perform that action at this time.