该系列为对Golang(go1.12.9)初入学习时,个人知识体系下认为重要部分的笔记整理。参考学习资料如下:
1、"The Go Programming Language" http://www.gopl.io
2、《Go语言实战》
3、极客时间《Go语言核心36讲》
更多学习可详见:《Go语言核心36讲》笔记2:Golang工作区和GOPATH
安装
golang项目:https://github.com/golang
window: https://download.youkuaiyun.com/psearch/0/10/0/2/1/go1.11.1.windows-amd64.msi 或者 https://studygolang.com/
IDE: Goland 或 idea+go plugin 或 https://blog.youkuaiyun.com/rudyn/article/details/73824963
环境变量与工作区
GOROOT:Go 语言安装根目录的路径,也就是 GO 语言的安装路径。
GOPATH:若干工作区目录的路径。是我们自己定义的工作空间。
GOBIN:GO 程序生成的可执行文件(executable file)的路径。
Go 语言源码的组织方式就是以环境变量 GOPATH、工作区、src目录和代码包为主线的,详见:代码组织
Go Modules(vgo)
在GoLand创建modules: https://www.jetbrains.com/help/go/create-a-project-with-vgo-integration.html
maven已经支持了go modules构建,参考:
<plugin>
<groupId>com.igormaznitsa</groupId>
<artifactId>mvn-golang-wrapper</artifactId>
<extensions>true</extensions>
<configuration>
<useEnvVars>true</useEnvVars>
<disableSSLcheck>true</disableSSLcheck>
<targetArch>amd64</targetArch>
<resultFolder>${product.path}</resultFolder>
<sources>${basedir}/src/go</sources>
<env>
<GOPROXY>${go.proxy}</GOPROXY>
<GO111MODULE>on</GO111MODULE>
</env>
</configuration>
<executions>
<execution>
<id>default-clean</id>
</execution>
<execution>
<id>default-build</id>
<phase>package</phase>
<configuration>
<resultName>${target.name}</resultName>
<targetOs>linux</targetOs>
<packages>
<main>./main.go</main>
</packages>
</configuration>
</execution>
<execution>
<id>windows-build</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
<configuration>
<resultName>${target.name}.exe</resultName>
<targetOs>windows</targetOs>
<packages>
<main>./main.go</main>
</packages>
</configuration>
</execution>
<execution>
<id>default-test</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
module是一个相关Go包的集合,它是源代码更替和版本控制的单元。模块由源文件形成的go.mod文件的根目录定义,包含go.mod文件的目录也被称为模块根。moudles取代旧的的基于GOPATH方法来指定在工程中使用哪些源文件或导入包。
详见:https://github.com/golang/go/wiki/Modules
也可以参照Tonybai的初窥Go module 一文,入手go module和理解Minimal Version Selection https://tonybai.com/2018/07/15/hello-go-module/
go modules最大进步在什么地方?从其原理和作用等维度看,欢迎留言讨论。