GoLand IDE 未正确编译。如何设置正确的路径?

Jea*_*anP 3 ide compiler-errors go goland

我正在尝试正确设置 GoLand,以便能够在 Go 中使用它。

我正在尝试运行以下简单的HelloWorld go 项目。

package HelloWorldProject


import "fmt"

func main(){
    fmt.Printf("Hello")

    fmt.Printf("1+1 = ", 1+1)

}
Run Code Online (Sandbox Code Playgroud)

这是我的控制台的结果:

GOROOT=/usr/local/Cellar/go/1.10/libexec #gosetup
GOPATH=/Users/jeanmac/go #gosetup
/usr/local/Cellar/go/1.10/libexec/bin/go build -i -o /private/var/folders/r5/rfwd1cqd4kv8cmh5gh_qxpvm0000gn/T/___Hello /Users/jeanmac/go/src/github.com/jpere547/HelloWorldProject/Hello.go #gosetup

Compilation finished with exit code 0
Run Code Online (Sandbox Code Playgroud)

我在 Mac OS 上,我使用 Brew 安装了 Go。

结果brew info go

go: stable 1.10 (bottled), HEAD
Open source programming language to build simple/reliable/efficient software
https://golang.org
/usr/local/Cellar/go/1.10 (8,150 files, 336.9MB) *
  Poured from bottle on 2018-03-22 at 19:38:29
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/go.rb
==> Requirements
Required: macOS >= 10.8 ?
==> Options
--without-cgo
    Build without cgo (also disables race detector)
--without-race
    Build without race detector
--HEAD
    Install HEAD version
==> Caveats
A valid GOPATH is required to use the `go get` command.
If $GOPATH is not specified, $HOME/go will be used by default:
  https://golang.org/doc/code.html#GOPATH

You may wish to add the GOROOT-based install location to your PATH:
  export PATH=$PATH:/usr/local/opt/go/libexec/bin
Run Code Online (Sandbox Code Playgroud)

GoLand 配置如下:
GOROOT GOROOT

路径 路径

dls*_*per 6

看起来您正在尝试运行非主程序包。具体来说,package HelloWorldProject您应该使用package main. 之后,IDE 不仅可以构建,还可以运行包。


小智 0

当你执行go build Something.gogo isntall Something.go时,它只会构建/安装 go 包来为你提供可执行文件。您将需要运行该可执行文件。

运行简单 Golang 程序的最简单方法就是使用go run some.go来运行你的 go 文件。

只要你的 GOPATH 设置正确,它就应该可以工作