Problems with Importing dependencies from Remote

I’m trying out some Go examples and I started with a JSON parser and I wanted to add a dependency to a remote repository. I was under the assumption that the Go build process will pull the dependencies when I compile, but strangely it did not. Here is the error that I get!

main.go:8:9: cannot find package "github.com/spf13/viper" in any of:
	/usr/local/Cellar/go/1.9.1/libexec/src/github.com/spf13/viper (from $GOROOT)
	/Users/joe/Projects/Private/go-projects/src/github.com/spf13/viper (from $GOPATH)

Here is my import:

import (
	"fmt"
	"log"
	"os"
	"encoding/json"
        "github.com/spf13/viper"
)

Any ideas as to what is wrong happening here? I have my GOPATH and GOROOT set up properly!

did you download it before?

go get github.com/spf13/viper

I thought the Go build will resolve this for me. Won’t it do that? If I have 10 or even 100’s of those dependencies from remote, should I do a go get for each one of them? This is purely inefficient. I’m sure I’m doing something wrong or? Is there a way that Go downloads it automatically when doing a Go build?

At the time of compiling Go will not resolve its dependencies, you can use a dependency manager, such as “glide” : “http://glide.sh/” or “Go dep” : “https://github.com/golang/dep” or any other

I like the glide, example:

glide.yaml

package: main
import:
- package: github.com/rs/cors
  version: ^1.0.0
- package: gopkg.in/mgo.v2
  subpackages:
  - bson
- package: github.com/gorilla/mux
- package: github.com/fatih/structs

and execute glide install

You can also go get yourpackage it will then get dependencies recursively

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.