美文网首页菜鸟学编程
Golang 入门 : 打造开发环境

Golang 入门 : 打造开发环境

作者: 菜鸟飞不动 | 来源:发表于2019-04-01 14:43 被阅读4次

    工欲善其事,必先利其器!在学习和使用 Golang 时如果有一款得心应手的 IDE,相信一定可以事半功倍。虽然很多 IDE 都提供了对 Golang 的支持,但真正好用的没几个。VSCode 算是不错的一个,但比起收费的 GoLand 还有些差距。本文结合 Golang 和 GoLand 的安装介绍如何构建一个基本的 go 语言开发环境。文中演示用户环境为 Ubuntu 18.04 desktop。

    安装 Golang

    Golang 的官网下载 Linux 版本的安装包,笔者下载到的最新的稳定版为 1.10.1。下载完成后检查一下完整性:
    $ sha256sum go1.10.1.linux-amd64.tar.gz

    image

    与下载页面中的 Checksum 核对无误后进入下面的安装步骤:

    • 解压缩安装包
    • 配置 PATH 变量
    • 运行 hello world

    解压缩安装包
    一般情况下我们会把 go 语言相关的工具安装在 /usr/local 目录下,当然你可以选择其它的目录。这里我们就选择 /usr/local 目录进行安装(其实就是个解压缩操作):

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">$ sudo tar -C /usr/local -xf go1.10.1.linux-amd64.tar.gz</pre>

    解压缩后安装也差不多就完成了,具体的目录为 /usr/local/go,目录下的内容如下:

    image

    配置 PATH 变量
    和 Linux 下的大多数应用程序一样,我们得为安装的 go 语言相关的程序配置 PATH 环境变量。具体做法就是把 /usr/local/go/bin 这个路径添加到 PATH 变量中:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">export PATH=$PATH:/usr/local/go/bin</pre>

    可以在不同的位置添加环境变量,比如 /etc/profile 或 HOME/.profile 等。不同之处是 /etc/profile 中的配置是共享给主机上的所有用户的,而HOME/.profile 中的配置只对当前用户起作用。笔者选择在 /etc/profile 中添加 PATH 变量的配置信息:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">$ sudo vim /etc/profile</pre>

    把上面的 export 语句添加到文件的最后一行:

    image

    保存该文本并退出登录,然后重新登录一次系统。执行下面的命令查看 PATH 变量的内容:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">echoPATH | grep go</pre>

    image

    看到 PATH 变量的输出中包含 /usr/local/go/bin 路径就说明 PATH 变量已经设置成功。也就是说,无论当前路径在哪里都可以直接执行 go 命令了:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">$ go</pre>

    image

    其实到这里我们已经完成了 go 语言开发环境的安装。下面我们来运行一个 hello world 程序。

    运行 hello world
    在 go 语言的开发环境中有工作区的(即 workspace)概念,你必须设置一个工作区,然后按照约定的目录等规则进行开发工作。如果我们没有设置工作区,go 语言的开发环境会认为我们在使用默认的工作区,即 $HOME/go 目录。当然你可以自己动手通过 GOPATH 环境变量设置一个更符合你个人习惯的目录作为工作区,这里我们使用默认的目录。
    按照约定,你的程序代码需要放置在工作区下的 src 目录下,所以在写代码前我们需要为我们的程序创建一个存放代码的目录:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">mkdir -p ~/go/src/hello && cd_</pre>

    go 语言代码文件的后缀为 .go,因此我们在 hello 目录下创建名为 hello.go 的源代码文件,并编辑其内容如下:

    [ 复制代码

    ](javascript:void(0); "复制代码")

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">package main

    import "fmt" func main() { fmt.Printf("hello, world\n")
    }</pre>

    [ 复制代码

    ](javascript:void(0); "复制代码")

    保存后执行下面的构建命令:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">$ go build</pre>

    这会在当前目录生成一个名为 hello 的可以执行文件:

    image

    执行 hello 程序:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">$ ./hello</pre>

    image

    输出的 "hello, world" 字符串证明我们安装的 go 开发环境是能够正常工作的!

    如果我们把 go build 命令换成 go install 命令呢?试试看:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">$ go install</pre>

    执行后当前目录中的 hello 可执行文件消失了,原来它被安装到了 ~/go/bin 目录下:

    image

    原来 go install 命令的作用就是在编译完成后把程序安装到工作区中的 bin 目录下。 在 go 语言的开发中,还有很多类似的约定,比如你引用的包默认会被安装在工作区中的 pkg 目录下,单元测试命令只认为以 _test.go 结尾的文件是测试文件等。不要困惑也不要惊讶,这就是 go 语言的设计理念,慢慢熟悉吧!

    安装 GoLand IDE

    jetbrains 官网下载最新的 GoLand 安装包,同样选择 Linux 版本,笔者下载到的版本为 goland-2018.1.2.tar.gz。下载完成后检查一下完整性(笔者下载的版本为 goland-2018.1.2.tar.gz):
    $ sha256sum goland-2018.1.2.tar.gz

    image

    与下载页面中的 Checksum 核对无误后进入下面的安装步骤:

    • 解压缩安装包
    • 执行安装脚本
    • 添加到 favorite bar
    • Run a demo

    解压缩安装包
    按照 GoLand 官方文档的建议,把安装包解压缩到 /opt 目录:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">$ sudo tar -C /opt -xf goland-2018.1.2.tar.gz</pre>

    执行安装脚本
    进入解压缩的目录下的 bin 目录中,然后执行 goland.sh 脚本:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">cd /opt/GoLand-2018.1.2/bin/ ./goland.sh //(此处不需要 sudo)</pre>

    在安装过程中需要一些 UI 交互,主要是确认用户协议和输入 license 信息(建议购买正版软件)。需要注意的是在安装完成后创建或打开项目前,请在 "Configure" 中选择 "Create Desktop Entry":

    image

    然后选择 New Project 创建一个名为 hrefcounter 的 demo 工程,并选择 Go 1.10.1 作为 SDK。

    添加到 favorite bar
    先关闭 GoLand IDE,为了今后能够方便的打开 GoLand,我们需要把它的图标添加到 Favorite bar 上。具体做法是,点击窗口左下角的 "Show Applications" 按钮,然后找到 GoLand 的桌面图标,右键该图标并选择 "Add to Favorites":

    image

    然后就可以通过点击 Favorite bar 上的图标来启动 GoLand 了:

    image

    Run a demo
    点击 Favorite bar 上的 GoLand 图标启动 GoLand IDE,打开我们刚才创建的 hrefcounter 项目。在项目的根目录下创建 app.go 文件,把这里的代码拷贝到 app.go 文件中并保存。然后按 Alt + F12 快捷键打开一个 GoLand 中的终端,并执行下面命令:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">$ go get -d -v golang.org/x/net/html</pre>

    上面的命令执行完成后,在按 Alt + Shift + F10 快捷键,并选择 "go build app.go" 开始执行程序:

    image

    该程序本身很简单,就是统计博客园主页上的链接数:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">{"internal":143,"external":36}</pre>

    如果返回类似上面的结果,说明程序执行完成,并成功返回,同时您的 Golang 开发环境也已经打造完成了!

    相关文章

      网友评论

        本文标题:Golang 入门 : 打造开发环境

        本文链接:https://www.haomeiwen.com/subject/bhjvbqtx.html