教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 go 安装与使用

go 安装与使用

发布时间:2022-02-04   编辑:jiaochengji.com
教程集为您提供go 安装与使用等资源,欢迎您收藏本站,我们将为您提供最新的go 安装与使用资源
<span style="font-family:楷体; line-height:1.428571em"><span style="line-height:1.428571em; font-size:15px">一 下载安装包</span></span>
<span style="line-height:1.428571em; font-size:15px">
</span>
<span style="line-height:1.428571em; font-size:15px">
</span>
<span style="line-height:1.428571em; font-size:15px">从http://code.google.com/p/go/downloads/list   网址下载</span>
<span style="line-height:1.428571em; font-size:15px">
</span>
<span style="line-height:1.428571em; font-size:15px"><span style="line-height:25.1875px; color:rgb(54,46,43); font-family:Helvetica,Tahoma,Arial,sans-serif">根据你的操作系统选择响应</span>的版合适版本,我选择的是 <span style="line-height:1.428571em; font-family:arial,sans-serif; background-color:rgb(221,248,204)">  </span>go1.1.2.windows-386.zip<span style="line-height:1.428571em; font-family:arial,sans-serif; background-color:rgb(221,248,204)">   32.7 MB</span></span>
<span style="line-height:1.428571em; font-family:arial,sans-serif; background-color:rgb(221,248,204); font-size:15px">
</span>
<span style="font-family:arial,sans-serif; line-height:1.428571em; font-size:15px">注:你也可以选择.msi文件安装,这样的话会自动的安装到c:/go目录下(这里不建议下载msi版本)</span>
<span style="line-height:1.428571em; font-family:arial,sans-serif; background-color:rgb(221,248,204); font-size:15px">
</span>
<span style="line-height:1.428571em; background-color:rgb(221,248,204); font-size:15px"><span style="font-family:楷体; line-height:1.428571em"><span style="line-height:1.428571em">二 解压</span></span></span>
<span style="font-family:arial,sans-serif; line-height:1.428571em; font-size:15px">
</span>
<span style="line-height:1.428571em; font-size:15px"><span style="font-family:arial,sans-serif; line-height:1.428571em">将下载后的文件解压,放到一个目录下,我把go放到了</span>D:\go目录下 </span>





<span style="font-family:楷体; line-height:1.428571em"><span style="line-height:1.428571em; font-size:15px">三设置环境变量</span></span>
<span style="line-height:1.428571em; font-size:15px">
</span>
<span style="line-height:1.428571em; font-size:15px"><span style="line-height:1.428571em">$GOROOT</span>  指向golang安装之后的根目录,如果你选择.msi安装的话,windows平台下默认为c:/go,而且在安装过程中由安装程序自动写入系统环境变量。
<span style="line-height:1.428571em">$GOARCH</span>  目标平台(编译后的目标平台)的处理器架构(386、amd64、arm)
<span style="line-height:1.428571em">$GOOS</span>      目标平台(编译后的目标平台)的操作系统(darwin、freebsd、linux、windows)
<span style="line-height:1.428571em">$GOBIN</span>    指向安装之后根目录下的bin目录,即$GOROOT/bin,windows平台下默认为c:/go/bin,会在安装过程中由安装程序自动添加到PATH变量中</span>

<span style="line-height:1.428571em; font-size:15px">对于我们来说只需要配置GOROOT和GOBIN即可(如果你下载的是msi安装后,就会自动写入环境变量,而无需我们配置)</span>
<span style="line-height:1.428571em; font-size:15px">右键我的电脑-->属性-->高级系统设置-->环境变量</span>





<span style="line-height:1.428571em">设置GOROOT</span>
<span style="line-height:1.428571em">
</span>


<span style="line-height:1.428571em">将bin路径添加到Path目录中</span>




<span style="line-height:1.428571em">打开终端运行 go version命令,如果出现如下图所示,说明安装成功</span>




<span style="line-height:1.428571em">四 编辑环境的搭建</span>
<span style="line-height:1.428571em">
</span>
<span style="line-height:1.428571em">
</span>
<span style="line-height:1.428571em"><span style="line-height:1.428571em; font-family:arial,sans-serif"><span style="line-height:1.428571em">1.首先需要设置</span></span><span style="line-height:25px; color:rgb(51,51,51); font-family:Helvetica,arial,freesans,clean,sans-serif">GOPATH</span><span style="line-height:1.428571em; font-family:arial,sans-serif">目录</span></span>
<span style="line-height:25px; color:rgb(51,51,51); font-family:Helvetica,arial,freesans,clean,sans-serif">
</span>
<span style="line-height:25px; color:rgb(51,51,51); font-family:Helvetica,arial,freesans,clean,sans-serif">go 命令依赖一个重要的环境变量:$GOPATH</span>
<span style="line-height:25px; color:rgb(51,51,51); font-family:Helvetica,arial,freesans,clean,sans-serif">注:</span>
<span style="line-height:1.428571em"><span style="line-height:25px; color:rgb(51,51,51); font-family:Helvetica,arial,freesans,clean,sans-serif">a.  这个不是Go安装目录,相当于我们的工作目录,</span>在类似 Unix 环境这样设置: export    GOPATH=/home/apple/mygo</span>
<span style="line-height:1.428571em; font-size:14px">b. <span style="line-height:25px; color:rgb(51,51,51); font-family:Helvetica,arial,freesans,clean,sans-serif">GOPATH允许多个目录,当有多个目录时,请注意分隔符,多个目录的时候Windows是分号,Linux系统是冒号,当有多个GOPATH时,默认会将go get的内容放在第一个目录下</span></span>
<span style="line-height:1.428571em">c<span style="line-height:25px; color:rgb(51,51,51); font-family:Helvetica,arial,freesans,clean,sans-serif">.  $GOPATH 目录约定有三个子目录:</span></span>
<span style="line-height:1.428571em"> </span>
src 存放源代码(比如:.go .c .h .s等)
pkg 编译后生成的文件(比如:.a)
bin 编译后生成的可执行文件(为了方便,可以把此目录加入到 $PATH 变量中,如果有多个gopath,那么使用${GOPATH//://bin:}/bin添加所有的bin目录)

<span style="line-height:1.428571em">
</span>
<span style="line-height:1.428571em">我们在D盘新建一个目录GoProject作为我们的gopath目录,并在目录中创建一个文件夹src,用来存放我们的源文件。</span>
<span style="line-height:1.428571em">
</span>
<span style="line-height:1.428571em">将goptah加入到环境变量中去:</span>
<span style="font-family:arial,sans-serif; line-height:1.428571em">
</span>


<span style="line-height:1.428571em">2.下载liteide http://code.google.com/p/golangide/downloads/list</span>

<span style="line-height:1.428571em">选择合适的版本,在此我们选择:<span style="line-height:1.428571em; font-family:arial,sans-serif; background-color:rgb(221,248,204)">  </span>liteidex19.windows.7z<span style="line-height:1.428571em; font-family:arial,sans-serif; background-color:rgb(221,248,204)">   14.3 MB</span></span>
<span style="font-family:arial,sans-serif; line-height:1.428571em">解压后运行</span>
<span style="line-height:1.428571em">
</span>
<span style="line-height:1.428571em"><span style="line-height:1.428571em">选择我们要运行的系统环境:</span></span>



<span style="line-height:1.428571em"><span style="line-height:1.428571em">将liteide中的系统环境变量设置为上面我们设置的</span></span>



<span style="line-height:1.428571em">在liteide中设置gopath:</span>



<span style="line-height:1.428571em"><span style="line-height:1.428571em">选择文件系统,然后在src下新建一个hello目录,然后在该目录下新建一个main.go文件:</span></span>


<span style="line-height:1.428571em">在main.go中编写代码:</span>
<span style="line-height:1.428571em">
</span>
<span style="line-height:1.428571em">
</span>
<pre style="margin-top:0px; margin-bottom:0px; padding:0px; border:0px; line-height:1.428571em; font-family:Monaco,Courier,monospace"><span style="line-height:1.428571em"><span style="line-height:1.428571em; color:rgb(0,0,128)">package</span><span style="line-height:1.428571em; color:rgb(192,192,192)"> </span>main </span> </pre> <pre style="margin-top:0px; margin-bottom:0px; padding:0px; border:0px; line-height:1.428571em; font-family:Monaco,Courier,monospace"><span style="line-height:1.428571em"> </span> </pre> <pre style="margin-top:0px; margin-bottom:0px; padding:0px; border:0px; line-height:1.428571em; font-family:Monaco,Courier,monospace"><span style="line-height:1.428571em"><span style="line-height:1.428571em; color:rgb(0,0,128)">import</span><span style="line-height:1.428571em; color:rgb(192,192,192)"> </span>( </span> </pre> <pre style="margin-top:0px; margin-bottom:0px; padding:0px; border:0px; line-height:1.428571em; font-family:Monaco,Courier,monospace"><span style="line-height:1.428571em"><span style="line-height:1.428571em; color:rgb(192,192,192)"> </span><span style="line-height:1.428571em; color:rgb(0,128,0)">"fmt"</span></span> </pre> <pre style="margin-top:0px; margin-bottom:0px; padding:0px; border:0px; line-height:1.428571em; font-family:Monaco,Courier,monospace"><span style="line-height:1.428571em">) </span> </pre> <pre style="margin-top:0px; margin-bottom:0px; padding:0px; border:0px; line-height:1.428571em; font-family:Monaco,Courier,monospace"><span style="line-height:1.428571em"> </span> </pre> <pre style="margin-top:0px; margin-bottom:0px; padding:0px; border:0px; line-height:1.428571em; font-family:Monaco,Courier,monospace"><span style="line-height:1.428571em"><span style="line-height:1.428571em; color:rgb(0,0,128)">func</span><span style="line-height:1.428571em; color:rgb(192,192,192)"> </span>main()<span style="line-height:1.428571em; color:rgb(192,192,192)"> </span>{ </span> </pre> <pre style="margin-top:0px; margin-bottom:0px; padding:0px; border:0px; line-height:1.428571em; font-family:Monaco,Courier,monospace"><span style="line-height:1.428571em"><span style="line-height:1.428571em; color:rgb(192,192,192)"> </span>fmt.Println(<span style="line-height:1.428571em; color:rgb(0,128,0)">"Hello</span><span style="line-height:1.428571em; color:rgb(192,192,192)"> </span><span style="line-height:1.428571em; color:rgb(0,128,0)">World"</span>)</span> </pre> <pre style="margin-top:0px; margin-bottom:0px; padding:0px; border:0px; line-height:1.428571em; font-family:Monaco,Courier,monospace"><span style="line-height:1.428571em">}</span> </pre> <pre style="margin-top:0px; margin-bottom:0px; padding:0px; border:0px; line-height:1.428571em; font-family:Monaco,Courier,monospace"><span style="line-height:1.428571em">打开终端输入:go install hello(注:需要以管理员的方式来运行)</span> </pre> <pre style="margin-top:0px; margin-bottom:0px; padding:0px; border:0px; line-height:1.428571em; font-family:Monaco,Courier,monospace"/> <pre style="margin-top:0px; margin-bottom:0px; padding:0px; border:0px; line-height:1.428571em; font-family:Monaco,Courier,monospace"/>

<span style="line-height:1.428571em"><span style="line-height:1.428571em"><span style="line-height:1.428571em">这时你会发现在GoProject中会自动新建了一个bin目录,里面有我们刚生成的exe文件</span></span></span>


<span style="color:#FF2600; line-height:1.428571em"><span style="line-height:1.428571em">注意:你也可以直接点击liteide中的BR运行程序,但是这样的话会在src目录下生成exe程序,这样不符合go工程的目录管理策略,所以我建议这样做。</span></span>


<span style="color:#FF2600; line-height:1.428571em"><span style="line-height:1.428571em">我们可以将liteide作为编辑器,当你编译和运行程序的时候最好在终端中使用go命令。</span></span>
<span style="color:#FF2600; line-height:1.428571em"><span style="line-height:1.428571em">下面我们来运行程序:</span></span>


<span style="line-height:1.428571em"><span style="line-height:1.428571em">我们也可以将D:GoProject\bin放到path环境变量里,这样的就可以直接运行程序了。</span></span>


到此这篇关于“go 安装与使用”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
【跟着我们学Golang】Go语言全平台安装
Windows环境下 Go 语言安装与环境变量配置
CentOS 7 安装 go 语言开发环境
GO安装(Linux,Mac,Windows)
ubuntu 16.04安装go语言并配置VSCode使用环境
Golang的SDK下载安装与Goland安装使用,创建第一个go程序
2018年最全Go语言教程零基础入门到进阶实战视频
Go语言开发环境安装
PHP单元测试工具PHPUnit安装详解
Golang 微服务 - 01 环境和工具

[关闭]
~ ~