佐敦 發表於 2019-8-12 15:23:00

go安装以及使用gomod、 goland设置

<p>一、 安装go</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;1. 官网下载go安装包,按照指引安装即可,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;2. 推荐使用go1.12版本 ,它新增加了go mod用来管理依赖,并且不需要我们再刻意设置gopath环境变量</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;3. 默认go会将$HOME/go目录作为gopath,所有项目下载的依赖包都会存放在这个目录下,我们不用再关心它。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;与python pip类似,默认pip会将$HOME/.pip这个目录作为缓存目录,go会将$HOME/go作为缓存目录,在这个目录同样可以更改,只需要在环境变量设置即可,如下:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;4. 由于墙的原因,部分官方依赖无法下载,我们可以使用代理,设置如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">linux:
export GOPATH</span>=/usr/local/<span style="color: rgba(0, 0, 0, 1)">gopath<br></span></pre>
<pre class="hljs objectivec"><code class="sh"><span class="hljs-keyword">export&nbsp;GOPROXY=https:<span class="hljs-comment">//goproxy.io<br><br><br></span></span></code></pre>
<pre><span style="color: rgba(0, 0, 0, 1)">windows: 添加环境变量<br>   GOPATH</span>=D:\gopath</pre>
<pre class="hljs objectivec"><code class="sh"><span class="hljs-keyword">   GOPROXY=https:<span class="hljs-comment">//goproxy.io</span></span></code></pre>
</div>
<p>&nbsp;</p>
<p>二、 使用go mod</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 1. 任意目录创建项目目录</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mkdir /home/mygo</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 2. 进入到项目目录</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cd&nbsp; /home/mygo</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 3.&nbsp;初始化生成<code>go.mod</code>&nbsp;文件</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;go mod&nbsp; init mygo&nbsp; &nbsp;(这里mygo名字也可以叫其他名字,一般为了与项目名称对应,就用项目名字)</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 4. 项目目录下会生成go.mod文件, 类似于python的requirements.txt文件。同时也生成一个go.sum文件,主要记载了下载包的哈希值用于校验,我们用不到。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 5.&nbsp;go.mod文件一旦创建后,它的内容将会被go toolchain全面掌控。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; go toolchain会在各类命令执行时,比如执行go get、go build、go run、go mod等命令时,自动修改和维护go.mod文件,这点跟pip还是有区别的</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 6.&nbsp;go.mod 提供了<code>module</code>,&nbsp;<code>require</code>、<code>replace</code>和<code>exclude</code>&nbsp;四个命令</p>
<ul>
<li><code>&nbsp; module</code>&nbsp;语句指定包的名字(路径)</li>
<li><code>&nbsp; require</code>&nbsp;语句指定的依赖项模块</li>
<li><code>&nbsp; replace</code>&nbsp;语句可以替换依赖项模块</li>
<li><code>&nbsp; exclude</code>&nbsp;语句可以忽略依赖项模块</li>
<li>
<div class="cnblogs_code">
<pre>$ <span style="color: rgba(0, 0, 255, 1)">cat</span><span style="color: rgba(0, 0, 0, 1)"> go.mod

module mygo

go </span><span style="color: rgba(128, 0, 128, 1)">1.12</span><span style="color: rgba(0, 0, 0, 1)">

require (
    github.com</span>/labstack/<span style="color: rgba(0, 0, 255, 1)">echo</span> v3.<span style="color: rgba(128, 0, 128, 1)">3.10</span>+incompatible <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> indirect</span>
    github.com/labstack/gommon v0.<span style="color: rgba(128, 0, 128, 1)">2.8</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> indirect</span>
    github.com/mattn/go-colorable v0.<span style="color: rgba(128, 0, 128, 1)">1.1</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> indirect</span>
    github.com/mattn/go-isatty v0.<span style="color: rgba(128, 0, 128, 1)">0.7</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> indirect</span>
    github.com/valyala/fasttemplate v1.<span style="color: rgba(128, 0, 128, 1)">0.0</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> indirect</span>
    golang.org/x/crypto v0.<span style="color: rgba(128, 0, 128, 1)">0.0</span>-<span style="color: rgba(128, 0, 128, 1)">20190313024323</span>-a1f597ede03a <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> indirect</span>
)</pre>
</div>
<p>&nbsp;</p>
</li>
</ul>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 7. 可以使用命令&nbsp;<code>go list -m -u all</code>&nbsp;来检查可以升级的package,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 使用<code>go get -u need-upgrade-package</code>&nbsp;升级后会将新的依赖版本更新到go.mod文件中。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 也可以使用&nbsp;<code>go get -u</code>&nbsp;升级所有依赖。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;8.&nbsp;由于某些已知的原因,并不是所有的package都能成功下载,比如:<code>golang.org</code>下的包。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;可以在 go.mod 文件中使用 replace 指令替换成github上对应的库,来下载相应的包。比如:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">replace (
    golang.org</span>/x/crypto v0.0.0-20190701094942-4def268fd1a4 =&gt; github.com/golang/cryptov0.0.0-20190701094942-4def268fd1a4</pre>
<pre><span style="color: rgba(0, 0, 0, 1)"> )<br><br>或者: <br>replace golang.org</span>/x/crypto v0.<span style="color: rgba(128, 0, 128, 1)">0.0</span>-<span style="color: rgba(128, 0, 128, 1)">20190701094942</span>-4def268fd1a4 =&gt; github.com/golang/crypto v0.<span style="color: rgba(128, 0, 128, 1)">0.0</span>-<span style="color: rgba(128, 0, 128, 1)">20190701094942</span>-4def268fd1a4</pre>
</div>
<p>&nbsp; &nbsp; &nbsp;9. 设置goland</p>
<p>&nbsp; &nbsp; &nbsp;&nbsp;https://www.cnblogs.com/congccy/p/10762257.html</p><br><br>
来源:https://www.cnblogs.com/wt11/p/11340386.html
頁: [1]
查看完整版本: go安装以及使用gomod、 goland设置