Golang 1.5 vendors - Cannot find package - go

Trying to build my project in go lang using version 1.5 with GO15VENDOREXPERIMENT="1" turned on to ensure I look for the vendors locally.
My structure is:
apps_api
main.go
build.sh
src
controllers
models
views
vendor
github.com
golang.org
.....
build.sh contains
export GO15VENDOREXPERIMENT="1"
export GOPATH=`pwd`
go build .
controller file example
import (
"models"
"views"
"github.com/gin-gonic/gin"
)
But i get lots of errors saying package not found see below for exmaple
src/controllers/app-versions.go:10:2: cannot find package "github.com/asaskevich/govalidator" in any of:
/Users/ereeve/.gvm/gos/go1.5/src/github.com/asaskevich/govalidator (from $GOROOT)
/Users/ereeve/Documents/gocode/src/apps_api/src/github.com/asaskevich/govalidator (from $GOPATH)
src/controllers/index.go:4:2: cannot find package "github.com/chnlr/baseurl" in any of:
/Users/ereeve/.gvm/gos/go1.5/src/github.com/chnlr/baseurl (from $GOROOT)
/Users/ereeve/Documents/gocode/src/apps_api/src/github.com/chnlr/baseurl (from $GOPATH)
If i add these lines into my build.sh file it will build, but I don't want to use go get because I am using go 1.5 with the vendors locally inside my project to avoid dependancies.
# go get github.com/gin-gonic/gin
# go get github.com/go-sql-driver/mysql
# go get github.com/rif/cache2go
....
Any ideas what I am doing wrong?

IIRC, GO15VENDOREXPERIMENT will work only if the package you're building is inside $GOPATH/src, so setting
export GOPATH=`pwd`
in your build.sh makes it fail. If you put your apps_api inside say ~/fakegopath/src/ and run
env GOPATH="${HOME}/fakegopath/src/" GO15VENDOREXPERIMENT="1" go build .
it should work.

Related

Error in compiling golang after migrating to centos

I was migrating my Golang programs from windows to Centos 7
It worked perfectly in Windows
but when I tried to compile on centos I get errors like
main.go:20:3: cannot find package "github.com/BurntSushi/toml" in any of:
/usr/local/go/src/github.com/BurntSushi/toml (from $GOROOT)
/root/work/src/github.com/BurntSushi/toml (from $GOPATH)
main.go:15:3: cannot find package "github.com/dgrijalva/jwt-go" in any of:
/usr/local/go/src/github.com/dgrijalva/jwt-go (from $GOROOT)
/root/work/src/github.com/dgrijalva/jwt-go (from $GOPATH)
main.go:16:3: cannot find package "github.com/gwlkm_service/config" in any of:
/usr/local/go/src/github.com/gwlkm_service/config (from $GOROOT)
/root/work/src/github.com/gwlkm_service/config (from $GOPATH)
kinda new to centos so idk what to do
looks like you have configured your GOPATH, without Go Module, you can use go get [package path] to download imported packages.
go get github.com/BurntSushi/toml
go get github.com/dgrijalva/jwt-go
go get github.com/gwlkm_service/config
Precisely
your go installation on server seems in /usr/local/go and
your project is in /root/work
so all your dependencies should be either in /root/work/src or /usr/local/go/src
now coming to action check your GOPATH with running echo $GOPATH
assuming it is automatically set to /usr/local/go/src
If not then follow - How do I SET the GOPATH environment variable on Ubuntu? What file must I edit?
If everything is ok then in your folder run go mod init
this will create mod file which will help you in further installations
look into - https://blog.golang.org/using-go-modules
then run go get commands as above #beiping96 said
go get github.com/BurntSushi/toml
go get github.com/dgrijalva/jwt-go
go get github.com/gwlkm_service/config
NOTE - after completing above process you will generate go modules file(same as package.json) and in future you won't need to care about dependencies

how to import package from github

I'm trying to build a api using Go and I tried running a existing file from GitHub that I got from udemy course. I'm getting this kind of output:
C:\goworkspace\src\grpc-go-course-master\blog\blog_client>go run client.go
client.go:4:2: cannot find package "context" in any of:
C:\go\src\context (from $GOROOT)
C:\goworkspace\src\context (from $GOPATH)
..\..\..\golang.org\x\net\http2\not_go111.go:10:2: cannot find package "net/http/httptrace" in any of:
C:\go\src\net\http\httptrace (from $GOROOT)
C:\goworkspace\src\net\http\httptrace (from $GOPATH)
Could you suggest me what can cause this error?
In order to download the dependencies, you have to use the go get command:
go get -v -u github.com/alessiosavi/GoUtils
Change with the package that you need (in that repo you can find a bunch of 'utils' method).
If the project, instead, have the go.mod file, you have to type:
go clean
go build
By this way you are going to install the dependencies listed in the go.mod file.
For update the dependencies to the latest version:
go get -v -u all
go mod tidy
The above command looks for missing dependencies, adds them to go.mod, and installs them.

Revel not working with GO15VENDOREXPERIMENT?

I'm using revel together with GO15VENDOREXPERIMENT. This implies that my revel package is located in the vendor folder inside my project folder:
$GOPATH\src\myproject\vendor\github.com\revel
Revel and my project build correctly. Go implicitly changes the revel imports to:
"myproject/vendor/github.com/revel/revel
However, when I go to run my project, revel tries to import from the standard GOPATH. It does not recognize that I have GO15VENDOREXPERIMENT turned on.
c:\WorkspaceGo\go\src\myproject>revel run myproject
~
~ revel! http://revel.github.io
~
ERROR 2015/12/20 09:51:27 revel.go:281: Failed to find Revel with error:
cannot find package "github.com/revel/revel" in any of:
C:\Go\src\github.com\revel\revel (from $GOROOT)
c:\WorkspaceGo\go\src\github.com\revel\revel (from $GOPATH)
How can I make revel recognize GO15VENDOREXPERIMENT so that it uses the correct GOPATH?

How do I install requirements in Go? "cannot find package"

I'm new to Go and I'm trying to set up a Go project with minimal documentation: https://github.com/alphagov/metadata-api
I've cloned it, but when I try go build I get the following warnings:
main.go:8:2: cannot find package "github.com/Sirupsen/logrus" in any of:
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/github.com/Sirupsen/logrus (from $GOROOT)
/Users/me/go/src/github.com/Sirupsen/logrus (from $GOPATH)
main.go:14:2: cannot find package "github.com/alphagov/metadata-api/content_api" in any of:
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/github.com/alphagov/metadata-api/content_api (from $GOROOT)
/Users/me/go/src/github.com/alphagov/metadata-api/content_api (from $GOPATH)
I'm guessing this is because I haven't installed the Go equivalent of requirements?
My GOPATH is set:
metadata-api$ echo $GOPATH
/Users/me/go
And the Go executable is in
metadata-ape$ echo $PATH
....:/Users/me/go/bin
What do I need to do to help Go find these packages?
You should install package first:
try
$ go get github.com/Sirupsen/logrus
and check you $GOPATH dir
This project use gom as the package manager,
Make sure you have installed gom
or try this command
$ gom install
I think your $GOPATH and $PATH settings are incorrect, the $GOPATH environment variable specifies the location of your workspace, these are my path settings:
export GOROOT=$HOME/bin/go
export GOBIN=$GOROOT/bin
export GOPATH=$HOME/golang
export PATH=$PATH:$GOBIN
I had similar issue and
export GO111MODULE=on
helped.
When you need your code to do something that might have been implemented by someone else (in Github or a package somewhere else), You should initialize a go mod file inside of your folder.)
For the purposes of this example, I'll just use example.com/module.
go mod init example.com/module
Add new module requirements and sums:
go mod tidy
Run your program:
go run .
For more details, see https://golang.org/doc/tutorial/getting-started.
Was able to fix the similar issue in Go 1.13.7 by typing:
export GOPATH=~/go
go get github.com/profile/repository
(e.g. github.com/Sirupsen/logrus)
"...Starting in Go 1.13, module mode will be the default for all development..."
"...When using modules, GOPATH is no longer used for resolving imports. However, it is still used to store downloaded source code (in GOPATH/pkg/mod) and compiled commands (in GOPATH/bin)..."

Build and install golang project with some internal packages

I have following golang project structure:
- go-projects
- src
- github.com
- user
- my-project
- pack
- pack.go
- main.go
my GOPATH is:
export GOPATH=/home/user/go-projects
I'm trying to call functions from pack package in main.go file and trying to build this project with:
cd my-project
go build && go-install
There is no any output, and also there is no bin directory anywhere. What i did wrong?
UPD This problem was solved. accidently missunderstood file content of main.go and pack.go. But now there is another problem.
How to build correctly this program. When i'm trying to execute go build in /home/user/go-projects/src/github.com/user/my-project i'm getting following erros:
main.go:4:8: cannot find package "github.com/user/pack" in any of:
/home/user/Downloads/go/src/pkg/github.com/user/pack (from $GOROOT)
/home/user/go-projects/src/github.com/user/pack (from $GOPATH)
Thank you
While it may be convenient to let the tools infer the package from the current working directory, it doesn't work for much more than a simple main package. Get used to referencing packages by their full import path, and you'll save yourself other problems down the line.
go install github.com/user/pack
It goes into $GOPATH/bin/my-project, if you wanna test it right away just use go run main.go

Resources