Programing

GOBIN이 설정되지 않았습니다. go install을 실행할 수 없습니다.

crosscheck 2020. 10. 14. 07:30
반응형

GOBIN이 설정되지 않았습니다. go install을 실행할 수 없습니다.


내 main.go 파일에 대한 사용자 지정 패키지를 설치하려고합니다. 그러나 내가 달렸을 때

go install custom.go

이 오류가 발생했습니다

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

GOBIN은 어떻게 설정하나요?


GOPATH변수를 확인하십시오 .
다음 사항을 확인하십시오.

  • 당신의 출처는 GOPATH/src
  • 당신은이 bin당신의 GOPATH 폴더 안에 폴더를.

참조 GOPATH 환경 변수 ( 'DIR'는 것입니다 GOPATH폴더) :

bin디렉토리는 컴파일 명령을 보유하고 있습니다.
각 명령은 소스 디렉토리의 이름이 지정되지만 전체 경로가 아닌 마지막 요소 만 지정됩니다. 즉, 소스가있는 명령 DIR/src/foo/quux이에 설치 DIR/bin/quux되지 않고에 설치됩니다 DIR/bin/foo/quux. 은 " foo/당신이 추가 할 수 있도록"접두어가 제거되고 DIR/bin사용자에 PATH설치된 명령에서 얻을 수 있습니다.

경우 GOBIN환경 변수가 설정되고, 명령은 디렉토리 대신 그 이름에 설치됩니다 DIR/bin. GOBIN절대 경로 여야합니다.


예를 들어, 이 스레드 는 go 빌드가 외부에서 수행되는 경우 어떤 일이 발생하는지 보여줍니다 GOPATH/src.

당신 같은 외모 GOPATH로 설정되어 ~/go있지만 실행 go install에 명령을~/dev/go

Go Build 보기

Go 경로는 Go 소스 코드를 포함하는 디렉토리 트리 목록입니다. 표준 Go 트리에서 찾을 수없는 가져 오기를 해결하기 위해 참조됩니다.

완료 한 경우 (아니오 )를 go build시도 할 수도 있습니다. 단일 파일이 아닌 패키지를 설치하려고합니다.go installcustom.go


나는 GOBIN 경로를 설정했고 그것은 나를 위해 일했습니다.

export GOBIN=[WorkspacePath]/bin

초보자로서 다양한 go 명령 (빌드, 실행 및 설치)을 시도 할 때이 오류가 발생했습니다. 즉, 당신은 할 수없는 설치 이동 filename.go을 . 패키지 만 설치할 수 있습니다.

다음을 배웠기 때문에 이것은 혼란 스러웠습니다.

nate:~/work/src/dir $ go run hello/hello.go
hello, world.

잘 작동합니다. 그러나 설치 가 작동하지 않는 이유를 알 수 없었습니다.

nate:~/work/src/dir $ go install hello/hello.go 
go install: no install location for .go files listed on command line (GOBIN not set)
nate:~/work/src/dir $ go install hello
can't load package: package hello: cannot find package "hello" in any of:
    /opt/go/src/hello (from $GOROOT)
    /home/ubuntu/work/src/hello (from $GOPATH)

내가 어떤 디렉토리에 있었 든 상관 없습니다.

nate:~/work/src/dir $ cd hello
nate:~/work/src/dir/hello $ go install hello.go 
go install: no install location for .go files listed on command line (GOBIN not set)
nate:~/work/src/dir/hello $ go install hello
can't load package: package hello: cannot find package "hello" in any of:
    /opt/go/src/hello (from $GOROOT)
    /home/ubuntu/work/src/hello (from $GOPATH)

이러한 혼란은 go run 이 Go 소스 파일 (.go로 끝나는 파일 이름) 에서만 작동하고 go install 은 패키지 허용하기 때문입니다. 패키지는 가져 오기 경로 또는 파일 시스템 경로로 이름이 지정됩니다. 그러므로:

nate:~/work/src/dir $ go install dir/hello
nate:~/work/src/dir $ go install ./hello/
nate:~/work/src/dir/hello $ go install .

모두 잘 작동합니다. 첫 번째는 가져 오기 경로로 패키지를 참조합니다 ($ GOPATH = "/ home / nate / work"라고 가정하면 go 도구는 / home / nate / work / src에서 소스 코드를 찾습니다). 나머지는 파일 시스템으로 해석됩니다. 선행 기간 때문에 경로.

GOPATH 문서 도 참조하십시오 .


실제로 두 가지 종류의 행동이 있습니다.

go install <package>

이것은 컴파일 및 패키지 및 종속성 설치에 문서화되어 있습니다. GOPATH를 올바르게 설정하면 GOBIN이 필요하지 않습니다.

go install <gofile>

이것은 문서화되어 있지 않으며이 모드에서는 GOBIN 환경 변수가 필요합니다.


이전 답변에서 지적했듯이 GOPATH 환경이 작업 공간에 올바르게 설정되어 있으면 GOBIN 환경 변수를 설정할 필요가 없습니다.

Please check your go environment variables by running $go env | grep -i "^GO" and look out for GOROOT and GOPATH to check if GOROOT is pointing to your GO source installation and GOPATH pointing to your workspace.

If everything is correct then navigate to the subdir where yourpkg.go resides then run $go build (without file name) first and $go install (again withour file name) second , if you don't see any error message on the screen your package is ready at your workspace/pkg/youros/../yourpackage.a


On windows with cygwin it seems to be a good idea to set up GOBIN to $GOPATH/bin.

and remember to properly escape the windows file name separator:

$ echo $GOROOT
C:\Go\

carl@rainier ~/gocode/src/github.com/user/hello
$ echo $GOPATH
C:\cygwin64\home\carl\gocode

carl@rainier ~/gocode/src/github.com/user/hello
$ echo $GOBIN
C:\cygwin64\home\carl\gocode\bin

For *nix system, look where go is installed, executing following command:

$ which go

which output let's say:

/usr/local/go/bin/go

then add following entries in ~/.bash_profile or in ~/.zshrc:

export GOROOT=/usr/local/go
export GOPATH=$GOROOT/src //your-go-workspace
export GOBIN=$GOROOT/bin //where go-generate-executable-binaries

PATH=$PATH:$GOPATH:$GOBIN

export PATH

P.S: Don't forget to source ~/.bash_profile or ~/.zshrc, as follows:

$ source ~/.bash_profile

Regarding setting GOBIN variable version not requiring it and just relying on GOPATH:

  1. GOBIN is required if we don't have a package, i.e. the file is directly in the GOPATH directory. This is likely when we are trying out the Go features as learners

  2. For typical Go projects, the files are under the package directories. For these, GOPATH is good enough.

  3. In other words, both the following solutions would work: a. Set GOBIN explicitly as $GOPATH/bin [only for learning purposes, can avoid] b. Create a subdirectory which would be your package name and move the .go files to it

  4. I guess Go utilities should remove the above error and handle the scenario better - based on whether the argument is a directory or a source file


You don't need $GOBIN if you've set a $GOPATH properly. If so, simply recheck if your project resides in the $GOPATH/src folder.


From https://golang.org/cmd/go/#hdr-Environment_variables:

GOBIN The directory where 'go install' will install a command.

and https://golang.org/cmd/go/#hdr-GOPATH_environment_variable:

If the GOBIN environment variable is set, commands are installed to the directory it names instead of DIR/bin. GOBIN must be an absolute path.

and https://golang.org/cmd/go/#hdr-Modules__module_versions__and_more

In module-aware mode, GOPATH no longer defines the meaning of imports during a build, but it still stores downloaded dependencies (in GOPATH/pkg/mod) and installed commands (in GOPATH/bin, unless GOBIN is set).

So, it seems basically you can use GOBIN to temporarily or permanently override the default binary install location (ie $GOPATH/bin). I had success installing a 1-file go "script" using env GOBIN=$HOME/bin/ go install testfile.go. This was done using go v1.11.

참고URL : https://stackoverflow.com/questions/25216765/gobin-not-set-cannot-run-go-install

반응형