Go through setup language and environment

download

download link

Go official website Download: https://golang.org/dl/

Go official mirror sites (recommended): https://golang.google.cn/dl/

installation

Windows Installation

In this installation example  64位Win10system installation  Go1.11.5可执行文件版本example.

The last step of the selected installation package downloaded to the local.

1550236584256

Double-click the downloaded file

1550236819662

1550236972659

1550237077339

Configuration GOPATH

GOPATHIt is an environment variable to indicate the storage path you go to write the project (working directory).

GOPATHThe best path set only one, all of the items code into GOPATHthe srcdirectory.

Additional information: After Go1.11 version, open go modafter the model is no longer mandatory to configure GOPATHa.

Linux and Mac platforms on the reference configuration environment variable way above to add your own working directory to the environment variable can be. Windows platform, follow the steps below to D:\code\goadd to environment variable:

1550293810242

1550293634570

1550293854247

1550294002369

1550294111480

1550294152856

1550296325263

 

Go project structure

Go during the time of language development, our code will always be stored in $GOPATH/srcthe directory. After the works go build, go installor go getthe like instructions, third party package will download the source code files in $GOPATH/srcthe directory, resulting in binary executable files  $GOPATH/bindirectory, the generated intermediate file will be stored in the cache  $GOPATH/pkg under.

If we use the version management tool (Version Control System, VCS. As usual Git) to manage our project code, we only need to add the $GOPATH/srcsource code to the directory. bin And  pkg directory content without having to version control.

 

Go Development Editor

Download and install

VS Code官方下载地址:https://code.visualstudio.com/Download

三大主流平台都支持,请根据自己的电脑平台选择对应的安装包。1550239338474双击下载好的安装文件,双击安装即可。

配置

安装中文简体插件

点击左侧菜单栏最后一项管理扩展,在搜索框中输入chinese ,选中结果列表第一项,点击install安装。

安装完毕后右下角会提示重启VS Code,重启之后你的VS Code就显示中文啦!Simplified Chinese plug-in installationVSCode主界面介绍:1550240342443

安装go扩展

现在我们要为我们的VS Code编辑器安装Go扩展插件,让它支持Go语言开发。Installation go extended map

第一个Go程序

Hello World

现在我们来创建第一个Go项目——hello。在我们的GOPATH下的src目录中创建hello目录。

在该目录中创建一个main.go文件:

package main  // 声明 main 包,表明当前是一个可执行程序

import "fmt"  // 导入内置 fmt 包

func main(){  // main函数,是程序执行的入口
	fmt.Println("Hello World!")  // 在终端打印 Hello World!
}

 

安装Go语言开发工具包

拷贝到自己电脑上的 GOROOT/bin 目录下。 go-tools百度云下载链接,密码:vjx2。 

go build

go build表示将源代码编译成可执行文件。

在hello目录下执行:

go build

  

或者在其他目录执行以下命令:

go build hello

  

go编译器会去 GOPATH的src目录下查找你要编译的hello项目

编译得到的可执行文件会保存在执行编译命令的当前目录下,如果是windows平台会在当前目录下找到hello.exe可执行文件。

可在终端直接执行该hello.exe文件:

d:\code\go\src\hello>hello.exe
Hello World!

  

我们还可以使用-o参数来指定编译后可执行文件的名字。

go build -o heiheihei.exe

go install

go install表示安装的意思,它先编译源代码得到可执行文件,然后将可执行文件移动到GOPATH的bin目录下。因为我们的环境变量中配置了GOPATH下的bin目录,所以我们就可以在任意地方直接执行可执行文件了。

跨平台编译

默认我们go build的可执行文件都是当前操作系统可执行的文件,如果我想在windows下编译一个linux下可执行文件,那需要怎么做呢?

只需要指定目标操作系统的平台和处理器架构即可:

SET CGO_ENABLED=0  // 禁用CGO
SET GOOS=linux  // 目标平台是linux
SET GOARCH=amd64  // 目标处理器架构是amd64

  

然后再执行go build命令,得到的就是能够在Linux平台运行的可执行文件了。

Mac 下编译 Linux 和 Windows平台 64位 可执行程序:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build

  

Linux 下编译 Mac 和 Windows 平台64位可执行程序:

CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build

  

Windows 64-bit compiler executable Mac platform:

SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build

  




原文地址:https://www.liwenzhou.com/posts/Go/install_go_dev/

Guess you like

Origin www.cnblogs.com/wqzn/p/11730052.html