如何在Windows上编译Go包?

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

How to compile a Go package on Windows?

问题

文档都是针对Mac OS X和Linux的,我想知道如何在Windows平台上编译Go包。在Windows上,我不知道如何编写make文件以及使用哪个工具来进行编译。

似乎没有一个名为make或go make的工具可以与Go开发工具的安装文件一起使用。

英文:

The documentation is all for Mac OS X and Linux, and I wish to know how to compile a Go package on the Windows platform. On Windows, I do not know how to write the make file and which tool to use to make it.

It seems that there is not a tool named make or go make to use with the installation file of Go development tools.

答案1

得分: 10

在Windows上编译Go包与在Linux或Mac OS X上编译Go包类似。使用go build命令。没有make文件。

以下是一些说明。

入门指南

如何编写Go代码

编译包和依赖项

英文:

Compiling a Go package on Windows is like compiling a Go package on Linux or Mac OS X. Use the go build command. There is no make file.

Here are some instructions.

Getting Started

How to Write Go Code

Compile packages and dependencies

答案2

得分: 5

在Go中不再需要Makefiles,所以make工具是不必要的。你也不需要cygwin。

如果你的Windows shell中似乎没有有效的go命令,那么请尝试按照在Windows上安装Go的官方文档进行操作。

> Zip压缩文件
>
> 将zip文件解压到您选择的目录中(我们建议c:\Go)。
>
> 如果您选择的目录不是c:\Go,则必须将GOROOT环境变量设置为您选择的路径。
>
> 将Go根目录的bin子目录(例如c:\Go\bin)添加到您的PATH环境变量中。
>
> MSI安装程序(实验性)
>
> 打开MSI文件并按照提示安装Go工具。默认情况下,安装程序将Go发行版放在c:\Go中。
>
> 安装程序应该将c:\Go\bin目录添加到您的PATH环境变量中。您可能需要重新启动任何打开的命令提示符才能使更改生效。
>
> 在Windows下设置环境变量
>
> 在Windows下,您可以通过“系统”控制面板的“高级”选项卡上的“环境变量”按钮来设置环境变量。某些版本的Windows通过“系统”控制面板内的“高级系统设置”选项提供此控制面板。

最后一节很重要。您的Windows PATH环境变量需要包含C:\Go\bin,这样您就可以在路径中使用go命令。

英文:

There are no more Makefiles needed in Go, so the make tool isn't necessary. You also do not need cygwin.

If you do not seem to have a valid go command in your windows shell, then try following the official docs on installing Go for windows

> Zip archive
>
> Extract the zip file to the directory of your choice (we suggest
> c:\Go).
>
> If you chose a directory other than c:\Go, you must set the GOROOT
> environment variable to your chosen path.
>
> Add the bin subdirectory of your Go root (for example, c:\Go\bin) to
> to your PATH environment variable.
>
> MSI installer (experimental)
>
> Open the MSI file and follow the prompts to install the Go tools. By
> default, the installer puts the Go distribution in c:\Go.
>
> The installer should put the c:\Go\bin directory in your PATH
> environment variable. You may need to restart any open command prompts
> for the change to take effect.
>
> Setting environment variables under Windows
>
> Under Windows, you may set environment variables through the
> "Environment Variables" button on the "Advanced" tab of the "System"
> control panel. Some versions of Windows provide this control panel
> through the "Advanced System Settings" option inside the "System"
> control panel.

The last section is important. Your windows PATH environment variable needs to have C:\Go\bin, so that you will have go in your path.

答案3

得分: 4

从:Golang windows,完整的设置指南http://noypi-linux.blogspot.com/2014/07/golang-windows-complete-setup-guide.html

1)下载ZIP

> 从以下网址获取最新代码:http://golang.org/dl/

2)解压ZIP

> 将zip文件解压到示例路径C:\local\dev\go

3)创建gopath目录

> Gopath是用于存储第三方库的位置。例如,如果要执行"go get github.com/somelib",该库将存储在gopath中。创建路径c:\local\dev\gopath

4)设置环境变量

> 打开系统属性->高级->环境变量

GOROOT=C:\local\dev\go
GOBIN=%GOROOT%\bin
GOPATH=c:\local\dev\gopath

5)将gobin添加到PATH

> 将C:\local\dev\go\bin追加到PATH

6)测试

6.1)创建路径"C:\local\dev\gopath\src\myfirstproject"

6.2)创建文件"C:\local\dev\gopath\src\myfirstproject\main.go"

package main

import "fmt"

func main() {
    fmt.Println("Hi foobar")
}

6.2)现在可以在任何位置构建项目,例如:

6.2.1)打开cmd.exe

6.2.2)cd c:\temp

6.2.3)go build myfirstproject

6.2.4)运行myfirstproject.exe

7)获取一些库

7.1)可以下载一些适用于Windows的免费git、svn和hg

7.2)一旦安装了这些库,可以执行"go get -u github.com/somelib"

8)获取一个IDE

> 下载liteide

恭喜!

英文:

from: Golang windows, a complete setup guide, http://noypi-linux.blogspot.com/2014/07/golang-windows-complete-setup-guide.html

1) download ZIP

> Get the latest code from: http://golang.org/dl/

2) extract ZIP

> Extract zip to example C:\local\dev\go

3) create a gopath directory,

> Gopath is where third parties will be stored. Example if you will
> execute a "go get github.com/somelib", this library will be stored in
> gopath. Create a c:\local\dev\gopath

4) set the environmental variables

> open System Properties->Advanced->Environmental Variables

GOROOT=C:\local\dev\go
GOBIN=%GOROOT%\bin
GOPATH=c:\local\dev\gopath

5) add your gobin to PATH

> append C:\local\dev\go\bin to PATH

6) test

6.1) create the path "C:\local\dev\gopath\src\myfirstproject"

6.2) create the main.go file "C:\local\dev\gopath\src\myfirstproject\main.go"

package main

import "fmt"

func main() {
    fmt.Println("Hi foobar")
}

6.2) you can now build the project anywhere example,

6.2.1) open cmd.exe

6.2.2) cd c:\temp

6.2.3) go build myfirstproject

6.2.4) run myfirstproject.exe

7) get a few libraries

7.1) you can download some free git, svn, and hg for windows

7.2) once you have them you can now do "go get -u github.com/somelib"

8) get an IDE

> download liteide

congrats!

1: http://noypi-linux.blogspot.com/2014/07/golang-windows-complete-setup-guide.html "Golang windows, a complete setup guide"

huangapple
  • 本文由 发表于 2012年7月4日 10:25:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/11321668.html
匿名

发表评论

匿名网友

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

确定