programing tip

Go 빌드 :“패키지를 찾을 수 없음”(GOPATH가 설정되어 있어도)

itbloger 2020. 8. 8. 11:39
반응형

Go 빌드 :“패키지를 찾을 수 없음”(GOPATH가 설정되어 있어도)


GOPATH제대로 설정 했음에도 불구하고 여전히 내 패키지를 찾기 위해 "go build"또는 "go run"을 얻을 수 없습니다. 내가 뭘 잘못하고 있죠?

$ echo $GOROOT
/usr/local/go

$ echo $GOPATH
/home/mitchell/go

$ cat ~/main.go
package main
import "foobar"
func main() { }

$ cat /home/mitchell/go/src/foobar.go
package foobar

$ go build main.go
main.go:3:8: import "foobar": cannot find package

foobar.go소스 파일이라는 디렉토리에 없기 때문에 작동하지 않습니다 foobar. go buildgo install디렉토리가 아닌 소스 파일과 일치하려고합니다.

  1. $GOPATH유효한 디렉토리로 설정하십시오 . 예 :export GOPATH="$HOME/go"
  2. 이동 foobar.go$GOPATH/src/foobar/foobar.go및 건물은 잘 작동합니다.

추가 권장 단계 :

  1. 추가 $GOPATH/bin사용자에 $PATH의하여 :PATH="$GOPATH/bin:$PATH"
  2. main.go의 하위 폴더로 이동 합니다. $GOPATH/src예 :$GOPATH/src/test
  3. go install test이제 터미널 $GOPATH/bin에 입력하여 호출 할 수 있는 실행 파일을 만들어야합니다 test.

편집 : GOPATH를 의미했기 때문에 fasmat답변을 참조하십시오 (upvoted)

" 내 패키지를 찾으려면 어떻게해야합니까? " 에서 언급했듯이 패키지 xxx를 디렉토리에 넣어야합니다 xxx.

Go 언어 사양을 참조하세요 .

package math

PackageName패키지 구현과 동일한 형식을 공유하는 파일 집합입니다 .
구현시 패키지의 모든 소스 파일이 동일한 디렉토리에 있어야합니다.

코드 조직은 언급 :

" widget" 패키지를 가져 오는 프로그램을 빌드 할 때 go명령은 src/pkg/widgetGo 루트 내부를 찾은 다음 (패키지 소스를 찾을 수없는 경우) src/widget각 작업 공간 내부를 순서대로 검색합니다 .

( "작업 공간"은 귀하의 경로 항목입니다 GOPATH. 해당 변수는 ' src, bin, pkg'가 될 여러 경로를 참조 할 수 있습니다)


(원래 답변)

또한 " Go 코드 작성 방법 "에 설명 된대로 GOPATH~ / go로 설정해야합니다 .GOROOT

Go 경로는 import 문을 해결하는 데 사용됩니다. go / build 패키지에 의해 구현되고 문서화됩니다.

GOPATH환경 변수를 나열 장소로 이동 코드를 볼 수 있습니다.
Unix에서 값은 콜론으로 구분 된 문자열입니다.
Windows에서 값은 세미콜론으로 구분 된 문자열입니다.
계획 9에서 값은 목록입니다.

다음과는 다릅니다 GOROOT.

The Go binary distributions assume they will be installed in /usr/local/go (or c:\Go under Windows), but it is possible to install them in a different location.
If you do this, you will need to set the GOROOT environment variable to that directory when using the Go tools.


TL;DR: Follow Go conventions! (lesson learned the hard way), check for old go versions and remove them. Install latest.

For me the solution was different. I worked on a shared Linux server and after verifying my GOPATH and other environment variables several times it still didn't work. I encountered several errors including 'Cannot find package' and 'unrecognized import path'. After trying to reinstall with this solution by the instructions on golang.org (including the uninstall part) still encountered problems.

Took me some time to realize that there's still an old version that hasn't been uninstalled (running go version then which go again... DAHH) which got me to this question and finally solved.


Have you tried adding the absolute directory of go to your 'path'?

export PATH=$PATH:/directory/to/go/

참고URL : https://stackoverflow.com/questions/13214029/go-build-cannot-find-package-even-though-gopath-is-set

반응형