在Windows机器上使用Go IDE,但在Linux机器上测试/运行应用程序?

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

Go IDE on Windows machine, but test/run application on Linux machine?

问题

我正在使用Go进行我的第一步。

我的工作站是Windows,我使用JetBrains IDE进行开发。通常,我会将网络磁盘(通过SSH)挂载到Linux机器上。这个环境对于编写和调试解释型语言(如PHP、Node.js和Python)非常方便。但对于编译型语言如Go来说,这个环境非常不友好。

为了编写Go代码,我使用了IntelliJ IDEA的Go插件。是否可以为这个插件定义一个远程Go编译器(在远程Linux机器上运行)?

英文:

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)?

答案1

得分: 2

由于Go是一种编译语言,所以这种开发环境的设置不像脚本语言那样容易。要在Windows下编译Linux二进制文件,您需要设置一个交叉编译环境。您可以从golang.org下载的二进制包只支持它们运行的平台(即Windows编译器只能生成Windows二进制文件),所以您需要从源代码编译Go。这篇博文对交叉编译进行了很好的介绍-请确保还阅读了“注意事项”部分,以防这些适用于您的情况。按照文章中描述的方式构建Go之后,您将拥有名为“go-linux-amd64”(“go-[os]-[architecture]”)的“go”工具的“克隆版本”,您可以使用它们来编译其他平台的二进制文件。

编辑: Dave Cheney在Go 1.5中改进的交叉编译方面写了另一篇博文(预计在8月发布)。

英文:

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).

答案2

得分: 0

你可以考虑即将发布的Go 1.5(2015年第三季度),它将使所有的交叉编译过程变得更加简单。"交叉编译环境"更容易获取,因为它完全基于Go。

Dave Cheney在"Cross compilation just got a whole lot better in Go 1.5"中详细介绍了这一消息。

要成功进行交叉编译,你需要:

  • 目标平台的编译器,如果它们与你的主机平台不同,例如你在darwin/amd64上(6g),想要为linux/arm(5g)进行编译。
  • 目标平台的标准库,其中包括在构建你的Go发行版时生成的一些文件。

随着将Go编译器转换为Go语言的计划在1.5版本中实现,第一个问题现在得到了解决。

以下是一个示例代码,用于在不同平台上构建和运行:

package main

import "fmt"
import "runtime"

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

要在darwin/386上构建:

% env GOOS=darwin GOARCH=386 go build hello.go
# 将文件复制到darwin主机
$ ./hello
Hello darwin/386

要在linux/arm上构建:

% env GOOS=linux GOARCH=arm GOARM=7 go build hello.go
# 将文件复制到linux主机
$ ./hello
Hello linux/arm

你可以在以下链接中找到更多相关信息:

Cross compilation just got a whole lot better in Go 1.5

Dave Cheney

Go compiler into Go

英文:

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

huangapple
  • 本文由 发表于 2014年3月24日 05:39:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/22597450.html
匿名

发表评论

匿名网友

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

确定