Joff
Joff

Reputation: 12177

go install fails if I am outside of the .go file directory

I installed Golang via the suggestion https://golang.org/doc/install and it seems I cannot run the go install command like they do on the website

If I run the command from any directory besides the directory that is home to the .go file, then it gives me this error. for example:

go install ./src/tutorial/helloworld/hello.go

or

go install ./path/to/.go/file/hello.go

go install: no install location for .go files listed on command line (GOBIN not set)

but if I run the install from inside the directory that has the .go file everything goes well and it places the final executable in the GOPATH bin folder.

//In the folder that contains my .go file
go install

I have set GOPATH in my .bash_profile and the go installation added the go root directory in /usr/local/go/bin to my PATH

Any ideas why I cannot run install from outside the directory like the instructions on the golang.org website says?

Upvotes: 2

Views: 4046

Answers (2)

user9505139
user9505139

Reputation: 1

I tried the chosen answer solution, but didn't work for me, given me the following error

cannot find main module, but found .git/config in 'path/to/root/of/project' to create a module there, run: cd .. && go mod init

When i searched for a bit, I found out that I need to git init in the folder where the mod exists, I don't know if I understood the solution, but I didn't want to add a git init as I have one in the root folder

Context " I want to run go install in root-folder/dir1/ "

I found using go help build

-C dir
Change to dir before running the command. Any files named on the command line are interpreted after changing directories. So what worked for me is

go install -C  ./path/to/.go/file

Upvotes: 0

Thundercat
Thundercat

Reputation: 120941

The arguments to the go install command are packages, not .go files. Use these commands to specify the package by relative path.

For the package containing the file ./src/tutorial/helloworld/hello.go:

go install ./src/tutorial/helloworld

For the package containing the file ./path/to/.go/file/hello.go:

go install ./path/to/.go/file

Upvotes: 3

Related Questions