Build and install golang project with some internal packages - go

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

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

Using modules, newly installed package cannot be referenced within project

go version go1.11.4 darwin/amd64
GOPATH has been unset but was previously set to $HOME/Development/go
Project path is under $HOME/code/
I’m able to successfully (no errors at least) install the go-sql-driver/mysql package using the command
go get github.com/go-sql-driver/mysql#v1
When I include a reference to that package in an import statement
import(
_ "github.com/go-sql-driver/mysql")
in my code I see the error
could not import github.com/go-sql-driver/mysql (can’t find import:
“github.com/go-sql-driver/mysql”)
I have run go mod init in my project root and it creates a go.mod file. When I run the go get command I see a require statement added to that file for the package. But it seems the files for the package get installed in the default $HOME/go directory (since I've unset GOPATH).
Should I be doing things differently so that my import statement can find the newly installed package? Using modules shouldn't all the packages be installed in the project path somewhere?
Should I be doing things differently so that my import statement can find the newly installed package?
No. With modules there is no need to install the packages upfront at all.
Using modules shouldn't all the packages be installed in the project path somewhere?
No. They get downloaded somewhere in some format and used from that location but they are not "installed" like in the old GOPATH variant of go get.
Show output of go env and what go mod vendor produces.
I'm pretty sure I was doing things wrong. I was able to resolve this after referencing the following closely the steps documented at golang modules wiki. The summary is that there is no need to "install" a package via 'go get'. Instead simply make sure your project is initialized to use modules using the 'go mod init' command and then include the package name in an import statement. The next build event will pull down the package and all its dependencies.

Problems with dependencies after setting up go dep

My project is located in $GOPATH/src/smp-cloudupload
I can run dep init without errors.
After that I cant compile the project anymore. One of the errors:
main/scs/scsApiGateway.go:5:2: cannot find package "_/home/dev/go/src/smp-cloudupload/vendor/github.com/dgrijalva/jwt-go" in any of:
/usr/lib/go-1.10/src/_/home/dev/go/src/smp-cloudupload/vendor/github.com/dgrijalva/jwt-go (from $GOROOT)
/home/dev/go/src/_/home/dev/go/src/smp-cloudupload/vendor/github.com/dgrijalva/jwt-go (from $GOPATH)
I am new to GO and I have a feeling, that there are either issues with my project structure or the GOPATH. The shown path is wrong. The correct path is: /home/dev/go/src/smp-cloudupload/vendor/github.com/dgrijalva/jwt-go
What am I doing wrong?
EDIT:
Output of echo $GOPATH:
dev#dev-VirtualBox:~/go/src/smp-cloudupload/main$ echo $GOPATH
/home/dev/go
I have no idea why there is a underscore in the path. The actual path of my project contains no underscores
I think this is resolved now. I was running into this problem:
https://github.com/Masterminds/glide/issues/602
After reading this:
https://thenewstack.io/understanding-golang-packages/
I setup my project structure as follows:
/home/dev/go/src/smp-cloudupload
pkg
src
main
somepackage
vendor
And it seems to work

Golang 1.5 vendors - Cannot find package

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.

Cannot install docker pkg dependency in Go

I'm trying to use the docker package in one of my Go applications. I'm importing the package as import "github.com/dotcloud/docker" in my script. But when trying to build the dependencies, that is, when I run go get in my project directory, it says:
foo.go:9:2: no buildable Go source files in /home/neville/gocode/src/github.com/dotcloud/docker
Here, my GOPATH is set to /home/neville/gocode, so when doing go get, the package should get downloaded to /home/neville/gocode/pkg, instead of /home/neville/gocode/src. What am I missing here?
github.com/dotcloud/docker isn't a Go package, and that's why there are no source files in that directory.
Import the package you want directly, like so for the registry package:
import "github.com/dotcloud/docker/registry"
Also, go get does download into $GOPATH/src. The installed object files go in $GOPATH/pkg.

Resources