Go代码构建错误,在标准包中使用了非标准导入 “fmt”,不允许导入循环。

huangapple go评论88阅读模式
英文:

Go code building error, non standard import "fmt" in standard package, import cycle not allowed

问题

我已经在Go语言中工作了一段时间,这个周末我想尝试部署一些服务器代码。当我在一些非常简单的代码上运行go build来测试Go是否正常工作时,我得到了这个错误:

无法加载包:($HOME)/go/src/goTest/main.go:4:2: 非标准导入“fmt”在标准包“goTest”中
不允许循环导入
包goTest
        导入fmt
        导入errors
        导入runtime
        导入runtime/internal/atomic
        导入unsafe
        导入runtime

在我的~/.bashrc文件中,我运行以下命令来设置环境变量。

export GOPATH=/usr/local/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN

我的Go工作空间在$HOME/go中,我的Go安装位于默认路径/usr/local/go。感谢任何帮助。

main.go

package main

import (
    "fmt"
)

func main() {
    fmt.Println("test")
}

编辑:go versiongo1.6 linux/amd64

英文:

I've been working in go for a little while and this weekend I wanted to try to deploy some server code. When I run go build on some very simple code to test if Go is working I get this error:

can't load package: ($HOME)/go/src/goTest/main.go:4:2: non-standard 
import "fmt" in standard package "goTest"
import cycle not allowed
package goTest
        imports fmt
        imports errors
        imports runtime
        imports runtime/internal/atomic
        imports unsafe
        imports runtime

In my ~/.bashrc I'm running this to set environment variables.

export GOPATH=/usr/local/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN

My go workspace is in $HOME/go and my go install is at the default /usr/local/go. Any help is appreciated

main.go

package main

import (
    "fmt"
)

func main() {
    fmt.Println("test")
}

Edit: go version is go1.6 linux/amd64

答案1

得分: 2

设置$GOPATH$GOROOT$GOBIN 正确地。在这种情况下,它们应该如下所示。

export GOPATH="$HOME/go"
export GOROOT="/usr/local/go"

此外,正确地定位源代码。也许它应该位于$GOPATH/src/github.com/<your id>/<repository name>/下面。

(如果您使用的是更新版本的Go(>=1.8),则不需要设置$GOPATH和其他变量。详细信息请参阅此处。

英文:

Set $GOPATH, $GOROOT, and $GOBIN correctly. In this case, they should be as follows.

export GOPATH=&quot;$HOME/go&quot;
export GOROOT=&quot;/usr/local/go&quot;

Also, locate the source code correctly. Maybe it should be under $GOPATH/src/github.com/&lt;your id&gt;/&lt;repository name&gt;/

(If you use newer version of Go (>=1.8), you don't have to set $GOPATH and others. Details here.)

答案2

得分: 0

我还没有看到这个,但是尝试修复你的路径:

  1. export GOPATH=$HOME/go

  2. export GOROOT=/usr/local/go

  3. mkdir -p $GOPATH/src/github.com/nubrozaref/goTest

  4. mv $GOPATH/src/goTest/ $GOPATH/src/github.com/nubrozaref/goTest

  5. 如果你想要的话:ln -s $GOPATH/src/github.com/nubrozaref/goTest ~/goTest

英文:

I haven't seen this, but try fixing your paths:

  1. export GOPATH=$HOME/go.

  2. export GOROOT=/usr/local/go.

  3. mkdir -p $GOPATH/src/github.com/nubrozaref/goTest

  4. mv $GOPATH/src/goTest/ $GOPATH/src/github.com/nubrozaref/goTest

  5. if you want: ln -s $GOPATH/src/github.com/nubrozaref/goTest ~/goTest

huangapple
  • 本文由 发表于 2017年5月1日 09:25:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/43713102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定