Link: cannot open file [Solved]


I have a problem (on windows and now on ubuntu, too). I have some libraries from github (among others cobra) and installed them via

go get ...

When I now want to build or run my application I get this message:

/usr/local/go/pkg/tool/linux_amd64/link: cannot open file /usr/local/go/pkg/linux_amd64/ open /usr/local/go/pkg/linux_amd64/ no such file or directory

/usr/local/go is the GOROOT
Which is absolutely correct since it is not there but under my GOPATH. I looked around now for a while but cannot solve that problem - Means, I solved the problem with copying the packages manually to GOROOT but this is not a real solution, I guess.

Do not set GOROOT. Do not copy stuff (other than the Go compiler) into /usr/local/go. Do show us the output of go env and go get -v -x $something.


I unset GOROOT and deleted all manually copied folders. Result:

09:36 $ go get -v -x

gGo env:

GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build381433808=/tmp/go-build -gno-record-gcc-switches"

Looks like it worked?

go get works. That is not the problem. But compiling does not work.

Is that the full output you got from go get -v -x We can’t see how the compiling does not work without seeing what happened.

Go-getting does compilation as part of it’s job. If go-get works, compilation works. But you’re not showing us any errors with your current setup, so it’s hard to say more.

I just removed all go stuff from my system and retried it. Here now the output from the go get:
    mkdir -p $WORK/
    cd /home/marczahn/go/src/
    /usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/ -trimpath $WORK -p -complete -buildid 6dbbadc32f1baf68add54ce5f60bddaf80bcc23c -D _/home/marczahn/go/src/ -I $WORK -I /home/marczahn/go/pkg/linux_amd64 -pack ./afero.go ./basepath.go ./cacheOnReadFs.go ./const_win_unix.go ./copyOnWriteFs.go ./httpFs.go ./ioutil.go ./memmap.go ./memradix.go ./os.go ./path.go ./readonlyfs.go ./regexpfs.go ./unionFile.go ./util.go
    mv $WORK/ /home/marczahn/go/pkg/linux_amd64/
    mkdir -p $WORK/
    cd /home/marczahn/go/src/
    /usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/ -trimpath $WORK -p -complete -buildid a8f8bec375d3dfb068ba9d5ca25174cc4aa18e4a -D _/home/marczahn/go/src/ -I $WORK -I /home/marczahn/go/pkg/linux_amd64 -pack ./bash_completions.go ./cobra.go ./command.go ./command_notwin.go
    mkdir -p /home/marczahn/go/pkg/linux_amd64/
    mv $WORK/ /home/marczahn/go/pkg/linux_amd64/
    mv $WORK/ /home/marczahn/go/pkg/linux_amd64/
    mv $WORK/ /home/marczahn/go/pkg/linux_amd64/
    mkdir -p /home/marczahn/go/pkg/linux_amd64/
    mv $WORK/ /home/marczahn/go/pkg/linux_amd64/
    mkdir -p $WORK/
    cd /home/marczahn/go/src/
    /usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/ -trimpath $WORK -p -complete -buildid 72cdf8bb335a3d424210efcb5722c48a617313a9 -D _/home/marczahn/go/src/ -I $WORK -I /home/marczahn/go/pkg/linux_amd64 -pack ./flags.go ./util.go ./viper.go
    mv $WORK/ /home/marczahn/go/pkg/linux_amd64/
    mkdir -p $WORK/
    mkdir -p $WORK/
    cd /home/marczahn/go/src/
    /usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/ -trimpath $WORK -p -complete -buildid dada704aef60b8ee09944503d14233e233678005 -D _/home/marczahn/go/src/ -I $WORK -I /home/marczahn/go/pkg/linux_amd64 -pack ./add.go ./helpers.go ./init.go ./licenses.go ./root.go
    mkdir -p /home/marczahn/go/pkg/linux_amd64/
    mv $WORK/ /home/marczahn/go/pkg/linux_amd64/
    mkdir -p $WORK/
    mkdir -p $WORK/
    cd /home/marczahn/go/src/
    /usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/ -trimpath $WORK -p main -complete -buildid 82a41c82fb6a953005296852663848fc5ce3e137 -D _/home/marczahn/go/src/ -I $WORK -I /home/marczahn/go/pkg/linux_amd64 -pack ./main.go
    cd .
    /usr/local/go/pkg/tool/linux_amd64/link -o $WORK/ -L $WORK -L /home/marczahn/go/pkg/linux_amd64 -extld=gcc -buildmode=exe -buildid=82a41c82fb6a953005296852663848fc5ce3e137 $WORK/
    mkdir -p /home/marczahn/go/bin/
    mv $WORK/ /home/marczahn/go/bin/cobra (download)
    cd .
    git clone /home/marczahn/go/src/
    cd /home/marczahn/go/src/
    git submodule update --init --recursive
    cd /home/marczahn/go/src/
    git show-ref
    cd /home/marczahn/go/src/
    git submodule update --init --recursive
    mkdir -p $WORK/
    mkdir -p $WORK/
    cd /home/marczahn/go/src/
    /usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/ -trimpath $WORK -p -complete -buildid 331a56d155d7f159deb74d4e0e7d5eb4117d636d -D _/home/marczahn/go/src/ -I $WORK -pack ./osext.go ./osext_go18.go
    mkdir -p /home/marczahn/go/pkg/linux_amd64/
    mv $WORK/ /home/marczahn/go/pkg/linux_amd64/

I am wondering why Go does not try to load them from GOPATH…

I kinda solved with providing the -pkgdir parameter but that is not really perfect…

Is your GOPATH not /home/marczahn/go? Your previous output of go env had GOPATH="/home/.../go/" I had hoped you were trying to hide your username and replaced it with .... Also, with g1.8 GOPATH defaults to $HOME/go, which seems to be what you are doing, so there is no need to actually set it.

What do you mean by “Go does not try to load them from GOPATH”? The go get output you provided seems to be using a GOPATH, putting binaries in $GOPATH/bin and compiled packages in $GOPATH/pkg like I expect.

What problem did you solve by providing -pkgdir?

Yeah - Tried to hide my username - Fail :smiley: And even not worth it…

Go seems to not use GOPATH when I want to compile my application (go build main.go) - Otherwise I would not have this error:

/usr/local/go/pkg/tool/linux_amd64/link: cannot open file /usr/local/go/pkg/linux_amd64/ open /usr/local/go/pkg/linux_amd64/ no such file or directory


This works:

go build -pkgdir /home/marczahn/go/pkg/ -o main main.go

What version of go are you using? How did you install it?

My attempts at replicating this problem have failed. I get more useful error messages like:

➜  ~ go build -v -o main main.go
main.go:5:2: cannot find package "" in any of:
	/usr/local/go/src/ (from $GOROOT)
	/home/alaster/go/src/ (from $GOPATH)
➜  ~ file /home/alaster/go
/home/alaster/go: cannot open `/home/alaster/go' (No such file or directory)

Notice that this tries to use the default GOPATH, which does not exist on that system.

Could you post a main.go that this problem occurs with and the path where you keep it?

A potentially relevant issue: (found by this search)

Indeed - do not use go build somefile.go. Many things will be different and confusing when you are giving it a single file. Use go build on packages, for example the package you are standing in when running the command:

$ go build -i -v

You should not need to use -pkgdir.

Go 1.8; I installed it as described on the official website -> Downloading, Moving to usr/local, setting up go path and create GOPATH-variable


package main

import (

var rootCmd *cobra.Command

func init() {
    config := application.Config()
    rootCmd = &cobra.Command{Use: "app"}
    rootCmd.PersistentFlags().BoolVarP(&config.Verbose, "verbose", "v", false, "verbose output")
    rootCmd.AddCommand(cmd.CmdServe(application.Config().HttpListen, application.UserMgmt(), application.Router()))

func main() {
    if err := rootCmd.Execute(); err != nil {

go build -v -o main main.go outout:

08:33 $ go build -v -o main main.go
# command-line-arguments
/usr/local/go/pkg/tool/linux_amd64/link: cannot open file /usr/local/go/pkg/linux_amd64/ open /usr/local/go/pkg/linux_amd64/ no such file or directory


08:35 $ go build -i -v
main.go:6:5: local import "./application" in non-local package
main.go:5:5: local import "./cmd" in non-local package

Strange thing is that it works fine on another system where we installed and get’ed the packages the same way

btw: Thank you very much for your help :slight_smile:

1 Like

I found now the solution - In fact the problem seems to be the relative imports. With

import "<project>/<package>"

it works as expected.

1 Like

Yes, do not use relative imports.

Sorry to be saying so much “do not” here, but you seem to have found a perfect storm of practices that are shunned or just not recommended when developing Go. :wink:

1 Like

Glad a way that works was found.


thank you guys for your time!

@calmh Absolutely fine - How should one learn otherwise? :slight_smile:

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