合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 目录结构 ### 目录结构 bin 存放编译后生成的可执行文件 pkg 存放编译后生成的归档文件 src 存放源码文件 ### 内置类型 slice map chan 属于引用类型(指针类型) ### 内置函数 init函数 用于包的初始化 每个包可以拥有多个init函数 ### init函数和main函数比较 相同点:定义时不能有任何的参数和返回值 且go自动调用 不同点:init可以应用于任意包中 且可以重复定义多个 main函数只能用于main包中 且只能定义一个 ### fmt.printin 和print区别 printin 输出控制台自带换行 print 不带换行 ``` <pre class="calibre14">``` 这样,一个使用Go包管理方式创建项目的小例子就完成了。 问题一:依赖的包下载到哪里了?还在<span class="token5">GOPATH</span>里吗? 不在。 使用Go的包管理方式,依赖的第三方包被下载到了$<span class="token5">GOPATH</span><span class="token">/</span>pkg<span class="token">/</span>mod路径下。 问题二: 依赖包的版本是怎么控制的? 在上一个问题里,可以看到最终下载在$<span class="token5">GOPATH</span><span class="token">/</span>pkg<span class="token">/</span>mod 下的包中最后会有一个版本号 v1<span class="token1">.</span><span class="token5">0.5</span>,也就是说,$<span class="token5">GOPATH</span><span class="token">/</span>pkg<span class="token">/</span>mod里可以保存相同包的不同版本。 版本是在go<span class="token1">.</span>mod中指定的。如果,在go<span class="token1">.</span>mod中没有指定,go命令会自动下载代码中的依赖的最新版本,本例就是自动下载最新的版本。如果,在go<span class="token1">.</span>mod用require语句指定包和版本 ,go命令会根据指定的路径和版本下载包, 指定版本时可以用latest,这样它会自动下载指定包的最新版本; 问题三: 可以把项目放在$<span class="token5">GOPATH</span><span class="token">/</span>src下吗? 可以。但是go会根据<span class="token5">GO111MODULE</span>的值而采取不同的处理方式,默认情况下,<span class="token5">GO111MODULE</span><span class="token">=</span>auto 自动模式 <span class="token1">[</span><span class="token5">1.</span>auto<span class="token1">]</span><span class="token1">(</span>https<span class="token1">:</span><span class="token">/</span><span class="token">/</span>link<span class="token1">.</span>zhihu<span class="token1">.</span>com<span class="token">/</span><span class="token">?</span>target<span class="token">=</span>http<span class="token">%</span><span class="token5">3</span>A<span class="token6">//1.auto)自动模式下,项目在$GOPATH/src里会使用$GOPATH/src的依赖包,在$GOPATH/src外,就使用go.mod 里 require的包</span> <span class="token5">2.</span>on 开启模式,<span class="token5">1.12</span>后,无论在$<span class="token5">GOPATH</span><span class="token">/</span>src里还是在外面,都会使用go<span class="token1">.</span>mod 里 require的包 <span class="token1">[</span><span class="token5">3.</span>off<span class="token1">]</span><span class="token1">(</span>https<span class="token1">:</span><span class="token">/</span><span class="token">/</span>link<span class="token1">.</span>zhihu<span class="token1">.</span>com<span class="token">/</span><span class="token">?</span>target<span class="token">=</span>http<span class="token">%</span><span class="token5">3</span>A<span class="token6">//3.off)关闭模式,就是老规矩。</span> 问题三: 依赖包中的地址失效了怎么办?比如<span class="token1">[</span>golang<span class="token1">.</span>org<span class="token">/</span>x<span class="token">/</span>…<span class="token1">]</span><span class="token1">(</span>https<span class="token1">:</span><span class="token">/</span><span class="token">/</span>link<span class="token1">.</span>zhihu<span class="token1">.</span>com<span class="token">/</span><span class="token">?</span>target<span class="token">=</span>http<span class="token">%</span><span class="token5">3</span>A<span class="token6">//golang.org/x/%25E2%2580%25A6)下的包都无法下载怎么办?</span> 在go快速发展的过程中,有一些依赖包地址变更了。以前的做法: <span class="token5">1.</span>修改源码,用新路径替换<span class="token2">import</span>的地址 <span class="token5">2.</span>git clone 或 go <span class="token2">get</span> 新包后,copy到$<span class="token5">GOPATH</span><span class="token">/</span>src里旧的路径下 无论什么方法,都不便于维护,特别是多人协同开发时。 使用go<span class="token1">.</span>mod就简单了,在go<span class="token1">.</span>mod文件里用 replace 替换包,例如 replace<span class="token1">[</span>golang<span class="token1">.</span>org<span class="token">/</span>x<span class="token">/</span>text<span class="token1">]</span><span class="token1">(</span>https<span class="token1">:</span><span class="token">/</span><span class="token">/</span>link<span class="token1">.</span>zhihu<span class="token1">.</span>com<span class="token">/</span><span class="token">?</span>target<span class="token">=</span>http<span class="token">%</span><span class="token5">3</span>A<span class="token6">//golang.org/x/text)\=>[golang/text](https://link.zhihu.com/?target=http%3A//github.com/golang/text)latest</span> 这样,go会用<span class="token1">[</span>github<span class="token1">.</span>com<span class="token">/</span>golang<span class="token">/</span>text<span class="token1">]</span><span class="token1">(</span>https<span class="token1">:</span><span class="token">/</span><span class="token">/</span>link<span class="token1">.</span>zhihu<span class="token1">.</span>com<span class="token">/</span><span class="token">?</span>target<span class="token">=</span>http<span class="token">%</span><span class="token5">3</span>A<span class="token6">//github.com/golang/text)[替代golang.org/x/text,原理就是下载github.com/golang/text](https://link.zhihu.com/?target=http%3A//%25E6%259B%25BF%25E4%25BB%25A3golang.org/x/text%25EF%25BC%258C%25E5%258E%259F%25E7%2590%2586%25E5%25B0%25B1%25E6%2598%25AF%25E4%25B8%258B%25E8%25BD%25BDgithub.com/golang/text)的最新版本到 $GOPATH/pkg/mod/golang.org/x/text下。</span> 问题四: init生成的go<span class="token1">.</span>mod的模块名称有什么用? 本例里,用 go mod init hello 生成的go<span class="token1">.</span>mod文件里的第一行会申明module hello 因为我们的项目已经不在$<span class="token5">GOPATH</span><span class="token">/</span>src里了,那么引用自己怎么办?就用模块名<span class="token">+</span>路径。 例如,在项目下新建目录 utils,创建一个tools<span class="token1">.</span>go文件<span class="token1">:</span> <span class="token2">package</span> utils <span class="token2">import</span> “fmt” func <span class="token3">PrintText</span><span class="token1">(</span>text string<span class="token1">)</span> <span class="token1">{</span> fmt<span class="token1">.</span><span class="token3">Println</span><span class="token1">(</span>text<span class="token1">)</span> <span class="token1">}</span> 在根目录下的hello<span class="token1">.</span>go文件就可以 <span class="token2">import</span> “hello<span class="token">/</span>utils” 引用utils <span class="token2">package</span> main <span class="token2">import</span> <span class="token1">(</span> <span class="token4">"hello/utils"</span> <span class="token4">"[astaxie/beego](https://link.zhihu.com/?target=http%3A//github.com/astaxie/beego)"</span> <span class="token1">)</span> func <span class="token3">main</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> utils<span class="token1">.</span><span class="token3">PrintText</span><span class="token1">(</span><span class="token4">"Hi"</span><span class="token1">)</span> <span class="token1">[</span>beego<span class="token1">.</span>Run<span class="token1">]</span><span class="token1">(</span>https<span class="token1">:</span><span class="token">/</span><span class="token">/</span>link<span class="token1">.</span>zhihu<span class="token1">.</span>com<span class="token">/</span><span class="token">?</span>target<span class="token">=</span>http<span class="token">%</span><span class="token5">3</span>A<span class="token6">//beego.Run)()</span> <span class="token1">}</span> ``` ```