【问题标题】:Go works from command line, but not from IntelliJ Idea GoGo 从命令行工作,但不能从 IntelliJ Idea Go
【发布时间】:2014-04-02 19:19:44
【问题描述】:

在 Mac OS X 下,我做了一个 brew install go。我还从这里下载了 Go IntelliJ IDE

http://go-ide.com/2011/08/09/goide_release_1_0_darwin.html

以下程序从命令行编译并运行良好,但如果我使用 GO IntelliJ IDE 则会出错:

can't find import: math/rand

导入时间似乎没问题,但 Duration 和 time.Millisecond 未定义。如果我删除这些东西并只运行“Hello World 版本”,程序就会编译并运行良好。

/**
 * Created by IntelliJ IDEA.
 * User: idf
 * Date: 4/2/14
 * Time: 1:59 PM
 * To change this template use File | Settings | File Templates.
 */

package main

import (
  "fmt"
  "math/rand" /* causes problems */
  "time"
)

func main() {

    sleep := time.Duration(200) /* error: undefined: time.Duration */
    time.Sleep(sleep * time.Millisecond) /* error: undefined: time.Millisecond */


    rand.Intn(1000)

    fmt.Println("Hello World")
}

【问题讨论】:

    标签: intellij-idea go


    【解决方案1】:

    更新:
    你需要添加你的 GOROOT 和 GOPATH: https://github.com/go-lang-plugin-org/go-lang-idea-plugin/blob/master/Missing%20ENV.md

    我运行的确切命令(显然,您的 GOPATH 和 GOROOT 会有所不同):

    launchctl setenv GOPATH /Users/wfreeman/gocode
    launchctl setenv GOROOT /Users/wfreeman/go1.2
    

    【讨论】:

    • 我对您的回复感到困惑。如果它可以找到 fmt 和时间,为什么添加这些路径会有所帮助?当我在一段时间后键入点时,Intellisense 向我显示了几件事,但 Duration 和毫秒都不存在。无论如何,我尝试了您的建议,但没有发现任何区别。
    • 看来brew安装的go和IDE中的go不一样。如果我从命令提示符说“which go”,它会给出 /usr/local/Cellar/go/1.2/bin 并且那里有一个完整的结构。 IDE 指向包 /Applications/Go Ide.app/bundle ...我不知道如何解决这个问题。
    • 您在进行这些更改后是否重新启动了 intellij?它对我有用。
    • 您应该使用 brew (/usr/local/Cellar/go/1.2) 中的 GOROOT。
    猜你喜欢
    • 2021-12-26
    • 2011-05-11
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2020-06-02
    • 1970-01-01
    相关资源
    最近更新 更多