質問

I'm doing my first steps with Go.

My workstation is on Windows and I'm using JetBrains IDE for development. Usually, I have mounted network disk (via SSH) to Linux machine. This environment it is pretty comfortable for coding and debugging with interpreted languages like PHP, Javascript (for Node), Python. But it is absolutely ugly for compiled languages like Go.

To write Go code I'm using the Go plugin for IntelliJ IDEA. Is it possible to define a remote Go compiler for this plugin (will run it on remote Linux machine)?

役に立ちましたか?

解決

Since Go is a compiled language, this kind of development setup is not as easy as with scripting languages. To compile a Linux binary under Windows, you have to set up a cross compilation environment. The binary packages you can download from golang.org only support the platforms they run on (i.e. the Windows compiler only produces Windows binaries), so you'll have to compile Go from source. This blog post gives a good introduction to cross compiling - make sure to also read the "caveats" section, just in case these apply to your situation. After building Go as described in the article, you will have "clones" of the "go" tool named "go-linux-amd64" ("go-[os]-[architecture]") which you can use to compile binaries for other platforms.

Edit: Dave Cheney has written another blog post on the much improved cross compilation available in Go 1.5 (which is due in August).

他のヒント

You can consider the upcoming Go 1.5 (Q3 2015), which will make the all cross-compilation process much simpler.
The "cross-compilation environment" is easier to get considering it is based purely on Go.

Dave Cheney details the news in "Cross compilation just got a whole lot better in Go 1.5"
)

For successful cross compilation you would need

  • compilers for the target platform, if they differed from your host platform, ie you’re on darwin/amd64 (6g) and you want to compile for linux/arm (5g).
  • a standard library for the target platform, which included some files generated at the point your Go distribution was built.

With the plan to translate the Go compiler into Go coming to fruition in the 1.5 release the first issue is now resolved.

package main

import "fmt"
import "runtime"

func main() {
        fmt.Printf("Hello %s/%s\n", runtime.GOOS, runtime.GOARCH)
}

build for darwin/386

% env GOOS=darwin GOARCH=386 go build hello.go
# scp to darwin host
$ ./hello
Hello darwin/386

Or build for linux/arm

% env GOOS=linux GOARCH=arm GOARM=7 go build hello.go
# scp to linux host
$ ./hello
Hello linux/arm
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top