Files
rikako-note/Golang/Golang Document.md

50 lines
1.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Golang
## Get Started
### Enable dependency tracking
当代码对其他module中包含的package进行了import时在自己的module中来管理依赖。
自己的module通过`go.mod`文件来定义,`go.mod`文件中会track项目所需要的依赖。
#### go mod init
`go mod init <module-name>`命令会创建一个`go.mod`文件,其中`<module-name>`会是module path。
在实际开发中module name通常是source code被保存的repository location例如`uuid`module的module name为`github.com/google/uuid`
#### go mod tidy
`go mod tidy`命令会根据import添加缺失的module并且移除未使用的module。
## multi-module workspace
示例目录结构如下所示:
- workspace
- workspace/hello
- workspace/example/hello
### go work init
在本示例中为了创建多module的workspace可以执行`go work init ./hello`,其会创建`go.work`文件,并将`./hello`目录下的module包含到`go.work`文件中。
`go.work`内容如下:
```
go 1.18
use ./hello
```
### go work use
通过`go work use ./example/hello`命令,会将`./example/hello`中的module加入到`go.work`文件中。
`go.work`内容如下:
```
go 1.18
use (
./hello
./example/hello
)
```
`go work use [-r] [dir]`命令行为如下:
- 如果指定目录存在,会为`dir``go.work`文件中添加一条use指令
- 如果指定目录不存在,会删除`go.work`文件中关于目录的use指令