您现在的位置是:首页 > 文章详情

安装Go,配置Go环境(实力亲测)

日期:2018-05-14点击:326

区块链研发

现在区块链项目大多都是用Go语言写的,其实语言都是想通的,如果你其他语言学习的好,那么你学习Go语言起来,也是非常简单的,如果你对区块链比较感兴趣,那么来学学Go也是一点问题都没有的。


一、了解一下Go

访问一下Go语言的官网
Go官网
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
Go是一门开源的编程语言,它能够让你更快的编译,更加可靠,编程效率更高。
这里写图片描述

二、下载Go

点击首页的Download Go进入下载页下载Go
这里写图片描述
选择你的操作系统对应的安装包

三、安装Go

  • 当你点击完第二部,就会开始下载对应的安装包,会打开一个新的安装指导页
    安装指导页面
    这里写图片描述
    mac的话,就双击打开刚刚下载的安装包,跟着步骤一步步安装即可。
  • 你的Go就安装在以下位置了
    The package installs the Go distribution to /usr/local/go.
    安装完成后,需要重新打开一个终端
/usr/local/go/bin ⌚ 18:13:11 $ ls -l total 59016 -rwxr-xr-x 1 root wheel 11199852 Mar 29 12:29 go -rwxr-xr-x 1 root wheel 15608476 Mar 29 12:37 godoc -rwxr-xr-x 1 root wheel 3403104 Mar 29 12:29 gofmt /usr/local/go/bin ⌚ 18:13:14 $ 

四、测试一下安装是否成功

  • Check that Go is installed correctly by setting up a workspace and building a simple program, as follows.
    (如果想要测试一下Go是否安装成功,需要在工作空间中编写一个小小的测试)
    Create your workspace directory, $HOME/go. (If you’d like to use a different directory, you will need to set the GOPATH environment variable.)
    (首先来创建一个工作空间,默认是在Home目录下的go目录,那么我现在不想在这个目录,我想重新换一个目录,请参考步骤五)
    Next, make the directory src/hello inside your workspace, and in that directory create a file named hello.go that looks like:
    (创建一个文件夹src/hello,在文件下创建一个名字为hello.go的文件)
package main import "fmt" func main() { fmt.Printf("hello, world\n") }

这里写图片描述

  • 编译这个文件
~/Block/go/hello ⌚ 18:33:19 $ go build
  • 编译成功后,会生成一个hello的可执行文件
~/Block/go/hello ⌚ 18:33:44 $ ls hello hello.go ~/Block/go/hello ⌚ 18:34:40 $ 
  • 运行一下这个文件
    输出hello,world就表示Go安装成功了。
~/Block/go/hello ⌚ 18:36:04 $ ./hello hello, world ~/Block/go/hello ⌚ 18:36:08 $ 
  • 看一下Go的版本号
~/Block/go/hello ⌚ 18:37:43 $ go version go version go1.10.1 darwin/amd64 ~/Block/go/hello ⌚ 18:37:52 $ 
  • 清除掉编译后的结果
~/Block/go/hello ⌚ 19:42:08 $ go clean ~/Block/go/hello ⌚ 19:42:11 $ ls hello.go

五、更换Go的工作空间位置

5.1 Bash

  • Bash
    Edit your ~/.bash_profile to add the following line:
    如果你用的是Bash配置,编辑Home目录下的.bash_profile文件,在其中添加一行。
    我的$HOME=/Users/wangdong
#go export GOPATH=$HOME/Block/go
  • 我想将我这个目录当作Go的工作空间
~/Block/go18:26:46 $ pwd /Users/wangdong/Block/go ~/Block/go18:26:48 $ 
  • 编辑完成后使配置生效
source ~/.bash_profile

5.2 Zsh

  • 如果你用的是Zsh
    Edit your ~/.zshrc file to add the following line:
export GOPATH=$HOME/go
  • export GOPATH=$HOME/go
export GOPATH=$HOME/go

六、卸载Go

  • 如果你现在不想用Go了,你想要卸载。
    只需要删除你系统中的Go的安装包即可
    This is usually /usr/local/go under Linux, Mac OS X, and FreeBSD or c:\Go under Windows.
    Linux、Mac OS X和FreeBSD在/usr/local/go目录;
    Windows 在c:\Go
  • 配置文件的清理
    进入第五步,清理掉Go的环境配置,将工作空间文件夹清理掉。
    卸载完成。

好啦,下面就可以愉快的开始Go语言的学习啦。

原文链接:https://yq.aliyun.com/articles/614501
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章