-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path170907-go教程-GOPATH配置与本地教程安装.html
47 lines (46 loc) · 19.4 KB
/
170907-go教程-GOPATH配置与本地教程安装.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!DOCTYPE html><html lang="zh-CN"><head><meta name="generator" content="Hexo 3.9.0"><meta http-equiv="content-type" content="text/html; charset=utf-8"><meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"><meta content="yes" name="apple-mobile-web-app-capable"><meta content="black-translucent" name="apple-mobile-web-app-status-bar-style"><meta content="telephone=no" name="format-detection"><meta name="description" content><title>go教程-GOPATH配置与本地教程安装 | zhiheng's blog</title><link rel="stylesheet" type="text/css" href="/css/style.css?v=1.0.0"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/normalize.css/normalize.min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/purecss/build/pure-min.min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/purecss/build/grids-responsive-min.css"><link rel="stylesheet" href="//cdn.jsdelivr.net/npm/[email protected]/css/font-awesome.min.css"><script type="text/javascript" src="//cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script><link rel="icon" mask sizes="any" href="/img/favicon.ico"><link rel="Shortcut Icon" type="image/x-icon" href="/img/favicon.ico"><link rel="apple-touch-icon" href="/apple-touch-icon.png"><link rel="apple-touch-icon-precomposed" href="/apple-touch-icon.png"><link rel="alternate" type="application/atom+xml" href="/atom.xml"><script type="text/javascript" src="//cdn.jsdelivr.net/npm/clipboard/dist/clipboard.min.js"></script><script type="text/javascript" src="//cdn.jsdelivr.net/gh/codeseven/toastr/build/toastr.min.js"></script><link rel="stylesheet" href="//cdn.jsdelivr.net/gh/codeseven/toastr/build/toastr.min.css"></head><body><div class="body_container"><div id="header"><div class="site-name"><h1 class="hidden">go教程-GOPATH配置与本地教程安装</h1><a id="logo" href="/.">zhiheng's blog</a><p class="description">喜于分享,勤于积累;欢迎关注我的微信公众号:治恒说说</p></div><div id="nav-menu"><a class="current" href="/."><i class="fa fa-home"> 首页</i></a><a href="/archives/"><i class="fa fa-archive"> 归档</i></a><a href="/about/"><i class="fa fa-user"> 关于</i></a><a href="/demo/"><i class="fa fa-square"> 有趣的代码</i></a></div></div><div class="pure-g" id="layout"><div class="pure-u-1 pure-u-md-3-4"><div class="content_container"><div class="post"><h1 class="post-title">go教程-GOPATH配置与本地教程安装</h1><div class="post-meta">2017年09月07日<script src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script><span id="busuanzi_container_page_pv"> | <span id="busuanzi_value_page_pv"></span><span> 阅读</span></span></div><div class="post-content"><blockquote>
<p>贵有恒,何必三更起五更眠。最无益,只怕一日曝十日寒。</p>
</blockquote>
<p>今天来学习 GOPATH 环境变量配置与工作空间,下一节将会学习到 GO 的一些常用命令。</p>
<h2 id="GOPATH-配置"><a href="#GOPATH-配置" class="headerlink" title="GOPATH 配置"></a>GOPATH 配置</h2><p>go 命令依赖一个重要的环境变量:$GOPATH </p>
<p>在类似 Unix 环境大概这样设置: <code>sh export GOPATH=/home/apple/mygo</code><br>Windows 设置如下,新建一个环境变量名称叫做 GOPATH: <code>GOPATH=c:\mygo</code> GOPATH允许多个目录,当有多个目录时,请注意分隔符,Windows是分号,Linux系统是冒号。当有多个 GOPATH 时,默认会将 go get 的内容放在第一个目录下</p>
<p>$GOPATH 目录约定有三个子目录:</p>
<ul>
<li>src 存放源代码(比如:.go .c .h .s等)</li>
<li>pkg 编译后生成的文件(比如:.a)</li>
<li>bin 编译后生成的可执行文件(为了方便,可以把此目录加入到 $PATH 变量中)</li>
</ul>
<p>GOPATH 是一些列用于go来查找包的目录列表. 使用 import “包名” 的时候, 如果在 GOROOT 里找不到, 应该会转向到你的 GOPATH 里去寻找.</p>
<p>注:GOPATH 和 GOROOT 不要设置在同一目录下,是因为不想你新安装的包, 污染了核心 go 的 pkg 和 src 文件。</p>
<h2 id="应用目录"><a href="#应用目录" class="headerlink" title="应用目录"></a>应用目录</h2><p>一般的,新建应用或者代码包时,都是在 src 目录下新建一个文件夹,文件夹的名称就是包名称,也可以有多级目录,例如:在 src 目录下新建了 $GOPATH/src/github.com/shure/mymath,那么这个包的路径就是<code>github.com/shure/mymath</code>,包名称是最后一级目录 <code>mymath</code></p>
<p>然后在 github.com/shure/mymath 下新建文件 <code>sqrt.go</code> 内容如下<br><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> mymath</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sqrt</span><span class="params">(x <span class="keyword">float64</span>)</span> <span class="title">float64</span></span> {</span><br><span class="line"> z := <span class="number">0.0</span></span><br><span class="line"> <span class="keyword">for</span> i := <span class="number">0</span>; i < <span class="number">1000</span>; i++ {</span><br><span class="line"> z -= (z*z - x) / (<span class="number">2</span> * x)</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> z</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>这样我们就创建了一个 mymath 的包,建议 package 的名称和目录名保持一致。</p>
<h2 id="编译应用"><a href="#编译应用" class="headerlink" title="编译应用"></a>编译应用</h2><p>编译应用 GO 提供了两种方式</p>
<p>1、进入对应的应用包目录,执行 go install 就可以安装了。</p>
<p>2、在任意目录执行 go install mymath</p>
<p>安装之后进入 pkg 目录下就可以看到 mymath.a 的文件,这个 .a 文件就是应用包。接下来我们来调用它:</p>
<p>新建应用 mathapp/main.go ,内容如下:<br><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line"> <span class="string">"fmt"</span></span><br><span class="line"> <span class="string">"github.com/shure/mymath"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line"> fmt.Printf(<span class="string">"Sqrt(2) = %v\n"</span>, mymath.Sqrt(<span class="number">2</span>))</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>然后进入该目录执行 <code>go build</code> 命令,就会在该目录下面生成一个 mathapp 可执行的文件,Linux 下运行 <code>sh ./mathapp</code>, Windows 下运行 <code>./mathapp.exe</code> 就会输出 <code>Sqrt(2) = 1.414213562373095</code> 。</p>
<p>继续执行 <code>go install</code> 命令安装,这时就会在 $GOPATH/bin/ 目录下生成一个 mathapp 的可执行文件,同样可以运行它,以 Windows 为例:<code>./mathapp.exe</code> 并打印 <code>Sqrt(2) = 1.414213562373095</code></p>
<h2 id="获取远程包"><a href="#获取远程包" class="headerlink" title="获取远程包"></a>获取远程包</h2><p>GO 提供了一个获取远程包的工具 <code>go get</code> ,目前go get支持多数开源社区(例如:<br>github、googlecode、bitbucket、Launchpad)</p>
<p>注意:使用这个命令必须安装对应开源平台的源码控制工具,例如:github 使用 git ,googlecode 使用 hg。</p>
<p>我们可以使用这个工具来获取 GO 官方提供的教程,安装至本地方便查阅,这对于国内用户来说是非常必要的。中文版教程 github 地址 <a href="https://github.com/Go-zh/tour" target="_blank" rel="noopener">https://github.com/Go-zh/tour</a> </p>
<p>直接在你 $GOPATH 的目录下运行 <code>$ go get -u github.com/Go-zh/tour/gotour</code> 等安装完成之后,会在 $GOPATH/bin 目录下生成一个 gotour 的可执行文件,运行该文件就会在你默认浏览器中打开教程。若你想安装该教程的英文版,直接执行 <code>go tool tour</code> 命令即可安装。</p>
<p>go get 本质上可以理解为首先第一步是通过源码工具 clone 代码到 src 下面,然后执行 go install 在代码中如何使用远程包,很简单的就是和使用本地包一样,只要在开头 import 相应的路径就可以 <code>import "github.com/shure/mymath"</code>.</p>
<h2 id="应用目录结构"><a href="#应用目录结构" class="headerlink" title="应用目录结构"></a>应用目录结构</h2><p>上面例子的目录结构</p>
<figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">$GOPATH</span><br><span class="line"> bin</span><br><span class="line"> |<span class="comment">--mathapp.exe</span></span><br><span class="line"> pkg</span><br><span class="line"> |<span class="comment">--windows_amd64 // 相应平台</span></span><br><span class="line"> |<span class="comment">--github.com</span></span><br><span class="line"> |<span class="comment">--shure</span></span><br><span class="line"> |<span class="comment">--mymath.a</span></span><br><span class="line"> src</span><br><span class="line"> |<span class="comment">--github.com</span></span><br><span class="line"> |<span class="comment">--Go-zh</span></span><br><span class="line"> |<span class="comment">--shure</span></span><br><span class="line"> |<span class="comment">--mathapp</span></span><br><span class="line"> |<span class="comment">--main.go </span></span><br><span class="line"> |<span class="comment">--mymath</span></span><br><span class="line"> |<span class="comment">--sqrt.go</span></span><br><span class="line"> |<span class="comment">--hello</span></span><br><span class="line"> |<span class="comment">--hello.go</span></span><br></pre></td></tr></table></figure>
<h2 id="参考与相关链接"><a href="#参考与相关链接" class="headerlink" title="参考与相关链接"></a>参考与相关链接</h2><p>参阅书籍:《Go Web 编程》</p>
<p>go 官方教程:<a href="https://tour.golang.org" target="_blank" rel="noopener">https://tour.golang.org</a></p>
<p>go 中文版教程本地安装:<a href="https://github.com/Go-zh/tour" target="_blank" rel="noopener">https://github.com/Go-zh/tour</a></p>
<p>go 中文版教程:<a href="http://tour.go-zh.org" target="_blank" rel="noopener">http://tour.go-zh.org</a></p>
<p>本教程 github 地址:<a href="https://github.com/dddreams/go-tutorial" target="_blank" rel="noopener">https://github.com/dddreams/go-tutorial</a></p>
<p><div style="text-align:center;margin:0;" markdown="1"><img src="../img/ddAnswer.jpg" alt="ddAnswer" style="margin:0 auto;"></div></p>
<p style="text-align: center;margin:0;">更多文章请关注微信公众号: zhiheng博客</p>
<p style="text-align: center;margin:20 0;">如果觉得文章对你有用,转发、分享、赞赏才是真爱 [斜眼笑]</p>
</div><div class="tags"><a href="/tags/go/"><i class="fa fa-tag"></i>go</a></div><div class="post-nav"><a class="pre" href="/170918-go教程-GO常用命令.html">go教程-GO常用命令</a><a class="next" href="/170904-go教程-go环境搭建.html">go教程-go环境搭建</a></div></div></div></div><div class="pure-u-1-4 hidden_mid_and_down"><div id="sidebar"><div class="widget"><div class="search-form"><input id="local-search-input" placeholder="Search" type="text" name="q" results="0"><div id="local-search-result"></div></div></div><div class="widget"><div class="widget-title"><i class="fa fa-wechat"> 微信公众号</i></div><div class="tagcloud"><img src="/img/zhihengss.jpg" style="width: 80%"></div></div><div class="widget"><div class="widget-title"><i class="fa fa-folder-o"> 分类</i></div><ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Github/">Github</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Mysql/">Mysql</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Python/">Python</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Spring-Boot/">Spring Boot</a><span class="category-list-count">6</span></li></ul></div><div class="widget"><div class="widget-title"><i class="fa fa-star-o"> 标签</i></div><div class="tagcloud"><a href="/tags/杂谈/" style="font-size: 15px;">杂谈</a> <a href="/tags/Kotlin/" style="font-size: 15px;">Kotlin</a> <a href="/tags/Google/" style="font-size: 15px;">Google</a> <a href="/tags/微信/" style="font-size: 15px;">微信</a> <a href="/tags/Photo/" style="font-size: 15px;">Photo</a> <a href="/tags/旅行/" style="font-size: 15px;">旅行</a> <a href="/tags/go/" style="font-size: 15px;">go</a> <a href="/tags/总结/" style="font-size: 15px;">总结</a> <a href="/tags/推荐/" style="font-size: 15px;">推荐</a> <a href="/tags/Spring-Boot/" style="font-size: 15px;">Spring Boot</a> <a href="/tags/Java/" style="font-size: 15px;">Java</a> <a href="/tags/运维/" style="font-size: 15px;">运维</a> <a href="/tags/Mysql/" style="font-size: 15px;">Mysql</a> <a href="/tags/Github/" style="font-size: 15px;">Github</a> <a href="/tags/教程/" style="font-size: 15px;">教程</a> <a href="/tags/软件/" style="font-size: 15px;">软件</a> <a href="/tags/Python/" style="font-size: 15px;">Python</a> <a href="/tags/Excel/" style="font-size: 15px;">Excel</a> <a href="/tags/工具/" style="font-size: 15px;">工具</a> <a href="/tags/JavaScript/" style="font-size: 15px;">JavaScript</a> <a href="/tags/Test/" style="font-size: 15px;">Test</a> <a href="/tags/Chrome/" style="font-size: 15px;">Chrome</a> <a href="/tags/Css/" style="font-size: 15px;">Css</a> <a href="/tags/Html/" style="font-size: 15px;">Html</a> <a href="/tags/设计模式/" style="font-size: 15px;">设计模式</a> <a href="/tags/Web/" style="font-size: 15px;">Web</a> <a href="/tags/React-Native/" style="font-size: 15px;">React-Native</a> <a href="/tags/ES6/" style="font-size: 15px;">ES6</a></div></div><div class="widget"><div class="widget-title"><i class="fa fa-file-o"> 最近文章</i></div><ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/211206-github精选-github加速访问神器.html">github精选-github加速访问神器</a></li><li class="post-list-item"><a class="post-list-link" href="/211204-github精选-一款高颜值的Redis客户端.html">github精选-一款高颜值的Redis客户端</a></li><li class="post-list-item"><a class="post-list-link" href="/211203-Python读取Excel中的图片(二).html">Python读取Excel中的图片(二)</a></li><li class="post-list-item"><a class="post-list-link" href="/211124-github精选-Linux命令大全.html">github精选-Linux命令大全</a></li><li class="post-list-item"><a class="post-list-link" href="/211120-github精选-PeaZip一款好用又免费的压缩软件.html">github精选-PeaZip一款好用又免费的压缩软件</a></li><li class="post-list-item"><a class="post-list-link" href="/211116-使用Python读取Excel中的图片并对应到记录.html">使用Python读取Excel中的图片并对应到记录</a></li><li class="post-list-item"><a class="post-list-link" href="/211111-SpringRestTemplate的使用.html">Spring RestTemplate的使用</a></li><li class="post-list-item"><a class="post-list-link" href="/211108-github精选-秒杀系统设计与实现.html">github精选-秒杀系统设计与实现</a></li><li class="post-list-item"><a class="post-list-link" href="/210208-Mysql登录失败多次锁定配置.html">Mysql登录失败多次锁定配置</a></li><li class="post-list-item"><a class="post-list-link" href="/210206-SpringBoot-使用JdbcTemplate操作数据库.html">SpringBoot-使用JdbcTemplate操作数据库</a></li></ul></div><div class="widget"><div class="widget-title"><i class="fa fa-external-link"> 友情链接</i></div><ul></ul><a href="https://geektutu.com/" title="极客兔兔" target="_blank">极客兔兔</a><ul></ul><a href="https://www.itmuch.com/" title="周立的博客" target="_blank">周立的博客</a><ul></ul><a href="https://liwenzhou.com/" title="李文周的博客" target="_blank">李文周的博客</a><ul></ul><a href="https://github.com/dddreams/dddreams.github.io/issues" title="添加友链请提交Issues" target="_blank">添加友链请提交Issues</a></div></div></div><div class="pure-u-1 pure-u-md-3-4"><div id="footer">Copyright © 2021 <a href="/." rel="nofollow">zhiheng's blog.</a> Powered by<a rel="nofollow" target="_blank" href="https://hexo.io"> Hexo.</a><a rel="nofollow" target="_blank" href="https://github.com/tufu9441/maupassant-hexo"> Theme</a> by<a rel="nofollow" target="_blank" href="https://github.com/pagecho"> Cho.</a></div></div></div><a class="show" id="rocket" href="#top"></a><script type="text/javascript" src="/js/totop.js?v=1.0.0" async></script><script type="text/javascript" src="//cdn.jsdelivr.net/gh/fancyapps/fancybox/dist/jquery.fancybox.min.js" async></script><script type="text/javascript" src="/js/fancybox.js?v=1.0.0" async></script><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox/dist/jquery.fancybox.min.css"><link rel="stylesheet" type="text/css" href="/css/search.css?v=1.0.0"><script type="text/javascript" src="/js/search.js?v=1.0.0"></script><script>var search_path = 'search.xml';
if (search_path.length == 0) {
search_path = 'search.xml';
}
var path = '/' + search_path;
searchFunc(path, 'local-search-input', 'local-search-result');
</script><script type="text/javascript" src="/js/copycode.js" successtext="复制成功!"></script><link rel="stylesheet" type="text/css" href="/css/copycode.css"><script type="text/javascript" src="/js/codeblock-resizer.js?v=1.0.0"></script><script type="text/javascript" src="/js/smartresize.js?v=1.0.0"></script></div></body></html>