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 build
및 go install
디렉토리가 아닌 소스 파일과 일치하려고합니다.
$GOPATH
유효한 디렉토리로 설정하십시오 . 예 :export GOPATH="$HOME/go"
- 이동
foobar.go
로$GOPATH/src/foobar/foobar.go
및 건물은 잘 작동합니다.
추가 권장 단계 :
- 추가
$GOPATH/bin
사용자에$PATH
의하여 :PATH="$GOPATH/bin:$PATH"
main.go
의 하위 폴더로 이동 합니다.$GOPATH/src
예 :$GOPATH/src/test
go install test
이제 터미널$GOPATH/bin
에 입력하여 호출 할 수 있는 실행 파일을 만들어야합니다test
.
편집 : GOPATH를 의미했기 때문에 fasmat 의 답변을 참조하십시오 (upvoted)
" 내 패키지를 찾으려면 어떻게해야합니까? " 에서 언급했듯이 패키지 xxx
를 디렉토리에 넣어야합니다 xxx
.
Go 언어 사양을 참조하세요 .
package math
PackageName
패키지 구현과 동일한 형식을 공유하는 파일 집합입니다 .
구현시 패키지의 모든 소스 파일이 동일한 디렉토리에 있어야합니다.
코드 조직은 언급 :
"
widget
" 패키지를 가져 오는 프로그램을 빌드 할 때go
명령은src/pkg/widget
Go 루트 내부를 찾은 다음 (패키지 소스를 찾을 수없는 경우)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
(orc:\Go
under Windows), but it is possible to install them in a different location.
If you do this, you will need to set theGOROOT
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
'programing tip' 카테고리의 다른 글
DOM ID에는 어떤 문자가 허용됩니까? (0) | 2020.08.08 |
---|---|
Moq : 재정의 할 수없는 구성원에 대한 잘못된 설정 : x => x.GetByTitle ( "asdf") (0) | 2020.08.08 |
추상 클래스의 모든 상속 된 클래스 가져 오기 (0) | 2020.08.07 |
psycopg2 커서에서 열 이름 목록을 얻으려면 어떻게해야합니까? (0) | 2020.08.07 |
Axios는 응답 헤더 필드에 액세스합니다 (0) | 2020.08.07 |