简体   繁体   English

如何在Windows上编译Go包?

[英]How to compile a Go package on Windows?

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. 该文档适用于Mac OS X和Linux,我希望知道如何在Windows平台上编译Go包。 On Windows, I do not know how to write the make file and which tool to use to make it. 在Windows上,我不知道如何编写make文件以及使用哪个工具来创建它。

It seems that there is not a tool named make or go make to use with the installation file of Go development tools. 似乎没有一个名为make或go的工具可以与Go开发工具的安装文件一起使用。

Compiling a Go package on Windows is like compiling a Go package on Linux or Mac OS X. Use the go build command. 在Windows上编译Go包就像在Linux或Mac OS X上编译Go包一样。使用go build命令。 There is no make file. 没有make文件。

Here are some instructions. 这是一些说明。

Getting Started 入门

How to Write Go Code 如何编写Go代码

Compile packages and dependencies 编译包和依赖项

There are no more Makefiles needed in Go, so the make tool isn't necessary. Go中不再需要Makefile,因此不需要make工具。 You also do not need cygwin. 你也不需要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 如果您的Windows shell中似乎没有有效的go命令,请尝试按照安装Go for windows的官方文档进行操作

Zip archive Zip档案

Extract the zip file to the directory of your choice (we suggest c:\\Go). 将zip文件解压缩到您选择的目录(我们建议使用c:\\​​ Go)。

If you chose a directory other than c:\\Go, you must set the GOROOT environment variable to your chosen path. 如果选择c:\\ Go以外的目录,则必须将GOROOT环境变量设置为所选路径。

Add the bin subdirectory of your Go root (for example, c:\\Go\\bin) to to your PATH environment variable. 将Go根目录的bin子目录(例如,c:\\ Go \\ bin)添加到PATH环境变量中。

MSI installer (experimental) MSI安装程序(实验性)

Open the MSI file and follow the prompts to install the Go tools. 打开MSI文件,然后按照提示安装Go工具。 By default, the installer puts the Go distribution in c:\\Go. 默认情况下,安装程序将Go分发放在c:\\ Go中。

The installer should put the c:\\Go\\bin directory in your PATH environment variable. 安装程序应将c:\\ Go \\ bin目录放在PATH环境变量中。 You may need to restart any open command prompts for the change to take effect. 您可能需要重新启动任何打开的命令提示才能使更改生效。

Setting environment variables under Windows 在Windows下设置环境变量

Under Windows, you may set environment variables through the "Environment Variables" button on the "Advanced" tab of the "System" control panel. 在Windows下,您可以通过“系统”控制面板的“高级”选项卡上的“环境变量”按钮设置环境变量。 Some versions of Windows provide this control panel through the "Advanced System Settings" option inside the "System" control panel. 某些版本的Windows通过“系统”控制面板内的“高级系统设置”选项提供此控制面板。

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. 你的Windows PATH环境变量需要有C:\\Go\\bin ,这样你才能go你的路径。

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

1) download ZIP 1)下载ZIP

Get the latest code from: http://golang.org/dl/ http://golang.org/dl/获取最新代码

2) extract ZIP 2)提取ZIP

Extract zip to example C:\\local\\dev\\go 将zip解压缩到示例C:\\ local \\ dev \\ go

3) create a gopath directory, 3)创建一个gopath目录,

Gopath is where third parties will be stored. Gopath是存储第三方的地方。 Example if you will execute a "go get github.com/somelib", this library will be stored in gopath. 例如,如果您将执行“go get github.com/somelib”,此库将存储在gopath中。 Create ac:\\local\\dev\\gopath 创建ac:\\ local \\ dev \\ gopath

4) set the environmental variables 4)设置环境变量

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 5)将你的gobin添加到PATH

append C:\\local\\dev\\go\\bin to PATH 将C:\\ local \\ dev \\ go \\ bin附加到PATH

6) test 6)测试

6.1) create the path "C:\\local\\dev\\gopath\\src\\myfirstproject" 6.1)创建路径“C:\\ local \\ dev \\ gopath \\ src \\ myfirstproject”

6.2) create the main.go file "C:\\local\\dev\\gopath\\src\\myfirstproject\\main.go" 6.2)创建main.go文件“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)您现在可以在任何地方构建项目示例,

6.2.1) open cmd.exe 6.2.1)打开cmd.exe

6.2.2) cd c:\\temp 6.2.2)cd c:\\ temp

6.2.3) go build myfirstproject 6.2.3)去构建myfirstproject

6.2.4) run myfirstproject.exe 6.2.4)运行myfirstproject.exe

7) get a few libraries 7)获得一些库

7.1) you can download some free git, svn, and hg for windows 7.1)你可以为windows下载一些免费的git,svn和hg

7.2) once you have them you can now do "go get -u github.com/somelib" 7.2)一旦你拥有它们,你现在可以做“go get -u github.com/somelib”

8) get an IDE 8)获得一个IDE

download liteide 下载liteide

congrats! 恭喜!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM