Go build language development environment (Goland and VSCode)

Tutorial starting at: micro-channel public number "Go programming time."

1. Download and install Go Language

Download: https: //golang.google.cn/dl/

After the download is complete, double-click the msi file to install, I used to install the software in my E drive Program Filesdirectory

Click all the way back is Nextto until the following screen appears, the installation is complete.

2. Configure Goland environment

Learn programming language, the use of a gratified IDE, you can help save a lot of trouble.

Python development project, I used to use PyCharm, as it has been accustomed to the style of the JetBrains IDE, you can go to cost a lot of familiar with the new IDE for the province, so here I still use JetBrains specifically for the Go language development IDE: Goland.

Goland Download: https: //download.jetbrains.com/go/goland-2019.2.3.exe

Double-click the downloaded exe file, in addition to select the installation path, I change into practice outside the E drive, select all the way Nextuntil the following interface, selected according to your needs full (recommended Select All)

Then all the way Nextuntil the following screen appears, the installation is complete, select Run GoglandRun Now.

At this point if you have not purchased an activation code if the JetBrains, can not be used at this time of Goland. At this point you can choose to try, or to purchase an activation code.

When your Goland normal use, create my project directory, the way to set up GOROOT.

After creating the Project, and then click on Files-> Settings-> GOPATH, add our project directoryF:\Go-Player

Just go click on a file, you can see the configuration of the inlet figure below arrow, click Go To Configuration runner.

Be configured as an indication.

Remove the parameter prompt

Set goproxy

设置 goimports(自动格式化插件),如果 你之前 没有安装 ,会提示你点击 yes 下载安装 。

至此,环境配置完成。

在项目根目录下,创建如下三个文件夹,并在 src 目录下创建一个hello.go 的文件。

点击运行按钮,在控制台我们看到了熟悉的 Hello, World!

3. 配置 VS Code 环境

提前设置用户级的环境变量

GOPATH = F:\Go-Player
PATH = %GOPATH%\bin  # 以追加的方式

昨天评论区有人问,GOPATH 和 GOROOT 是什么?为什么需要设置?回想一下 你学 Python 的话,安装 Python 解释器的时候,是不是也要设置环境变量?这里也是类似。

GOROOT:在GO语言中表示的是 Go语言编译、工具、标准库等的安装路径,通过它可以告诉系统你的 go.exe 是放在哪里,不设置的话,你后面执行 go getgo install 的时候,系统就不认识它了。

GOPATH环境变量则表示 Go的工作目录,这个目录指定了需要从哪个地方寻找GO的包、可执行程序等,这个目录可以是多个目录表示。这里我设置成我的工作空间(目录你可以自己定) :F:\Go-Player,如果不设置的话 ,默认是在你的用户目录下的 go 文件夹。

由于某些原因,在国内访问不了 Go 官方的源,这导致我们在下载一些包的时候,会下载失败。

解决方法有很多,目前来讲,最简单的是配置一个代理。

具体怎么配置呢? 只要一条命令就行了。

$ go env -w GOPROXY=https://goproxy.cn,direct

这时要再说一点,GO 项目中,一般来说它的工作目录结构是这样的:

  • bin目录:包含了可执行程序,注意是可执行的,不需要解释执行。
  • pkg目录:包含了使用的包或者说库。
  • src目录:里面包含了go的代码源文件,其中仍按包的不同进行组织。

所以后面我的创建的GO工作目录,也是按照这个标准来,先说明一下。

接下来,要开始配置 VS Code 环境。

打开你的 VS Code软件,先确认你设置的环境变量已经生效,点击 Terminal -> New Terminal,使用 cmd 命令查看环境变量。

如上图所求,我的环境变量是OK的,如果你的输出是指向你的用户目录:%USERPROFILE%\go 建议你不要折腾(因为我无论重启多少次 VS Code,其记录的GOPATH始终指向%USERPROFILE%\go), 直接重启你的电脑。

好了之后,我们要从 github 上下载两个仓库,之所以要手动下载,是因为有墙的存在,在线安装的话,很多插件你会下载失败。

创建目录 src/goland.org/x/,并进入此目录,执行命令

$ git clone https://github.com/golang/tools.git
$ git clone https://github.com/golang/lint.git

点击 File - Open Folder 安装两个插件:

第一个是:Go 语言的扩展插件

第二个是:Code Runner,让你的 VS Code 能够编译运行 Go 的程序。

随便点开一个 go 文件,在你的右下角会提示要你安装一些工具,点击 Install All

然后你在 OUTPUT 就能看到安装进度

安装的 exe 文件会放在 %GOPATH%/bin 下,也就是 F:\Go-Player\bin

而此的 src 目录结构是这样的

到这时环境配置完成,编写 HelloWorld,并运行查看输出,一切完成。


发布了10 篇原创文章 · 获赞 10 · 访问量 1369

Guess you like

Origin blog.csdn.net/weixin_36338224/article/details/104174165