Skip to content

Commit e813e21

Browse files
committed
增加 golang 杂谈
1 parent 9d52890 commit e813e21

File tree

4 files changed

+577
-0
lines changed

4 files changed

+577
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 如此,用dep获取私有库
2+
3+
## 介绍
4+
`dep`是一个依赖管理工具。它需要1.9或更新的`Golang`版本才能编译
5+
6+
`dep`已经能够在生产环节安全使用,但还在官方的试验阶段,也就是还不在`go tool`中。但我想是迟早的事 :=)
7+
8+
指南和参考资料,请参阅[文档](https://golang.github.io/dep/)
9+
10+
11+
## 获取私有库
12+
我们常用的`git`方式有两种,第一种是通过`ssh`,第二种是`https`
13+
14+
本文中我们以`gitlab.com`为案例,创建一个`private`的私有仓库
15+
16+
### 通过`ssh`方式
17+
18+
首先我们需要在本机上生成`ssh-key`,若没有生成过可右拐[传送门](https://segmentfault.com/a/1190000013450267)
19+
20+
得到需要使用的`ssh-key`后,我们打开我们的`gitlab.com`,复制粘贴入我们的`Settings` -> `SSH Keys`
21+
22+
![image](https://sfault-image.b0.upaiyun.com/105/328/1053288135-5a96d458e9145)
23+
24+
添加成功后,我们直接在`Gopkg.toml`里配置好我们的参数
25+
```
26+
[[constraint]]
27+
branch = "master"
28+
name = "gitlab.com/eddycjy/test"
29+
source = "[email protected]:EDDYCJY/test.git"
30+
```
31+
32+
在拉取资源前,要注意假设你是第一次用该`ssh-key`拉取,需要先手动用`git clone`拉取一遍,同意`ECDSA key fingerprint`
33+
```
34+
$ git clone [email protected]:EDDYCJY/test.git
35+
Cloning into 'test'...
36+
The authenticity of host 'gitlab.com (52.167.219.168)' can't be established.
37+
ECDSA key fingerprint is xxxxxxxxxxxxx.
38+
Are you sure you want to continue connecting (yes/no)? yes
39+
...
40+
```
41+
42+
接下来,我们在项目下**直接执行`dep ensure`就可以拉取**下来了!
43+
44+
#### 问题
45+
46+
1. 假设你是第一次,又没有执行上面那一步(`ECDSA key fingerprint`),会一直卡住
47+
48+
2. 正确的反馈应当是执行完命令后没有错误,但如果出现该错误提示,那说明该存储仓库没有被纳入`dep`中(例:`gitee`
49+
```
50+
$ dep ensure
51+
52+
The following issues were found in Gopkg.toml:
53+
54+
unable to deduce repository and source type for "xxxx": unable to read metadata: go-import metadata not found
55+
56+
ProjectRoot name validation failed
57+
```
58+
59+
60+
### 通过`https`方式
61+
62+
我们直接在`Gopkg.toml`里配置好我们的参数
63+
```
64+
[[constraint]]
65+
branch = "master"
66+
name = "gitlab.com/eddycjy/test"
67+
source = "https://{username}:{password}@gitlab.com"
68+
```
69+
70+
主要是修改`source`的配置项,`username`填写在`gitlab`的用户名,`password`为密码
71+
72+
最后回到项目下**执行`dep ensure`拉取资源**就可以了
73+
74+
## 最后
75+
76+
`dep`目前还是官方试验阶段,还可能存在变数,多加注意
77+
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Go依赖管理工具
2+
>Go dependency management tool
3+
## 环境要求
4+
- Golang >= 1.9
5+
- Dep
6+
7+
## 目前版本:
8+
```
9+
dep:
10+
version : devel
11+
build date :
12+
git hash :
13+
go version : go1.10
14+
go compiler : gc
15+
platform : linux/amd64
16+
```
17+
`Latest release``v0.4.1`
18+
19+
## 安装
20+
```
21+
go get -u github.com/golang/dep/cmd/dep
22+
```
23+
`$GOPATH/bin`不在`PATH`下,则需要将生成的`dep`文件从`$GOPATH/bin`移动至`$GOBIAN`
24+
25+
## 验证
26+
```
27+
$ dep
28+
Dep is a tool for managing dependencies for Go projects
29+
30+
Usage: "dep [command]"
31+
32+
Commands:
33+
34+
init Set up a new Go project, or migrate an existing one
35+
status Report the status of the project's dependencies
36+
ensure Ensure a dependency is safely vendored in the project
37+
prune Pruning is now performed automatically by dep ensure.
38+
version Show the dep version information
39+
40+
Examples:
41+
dep init set up a new project
42+
dep ensure install the project's dependencies
43+
dep ensure -update update the locked versions of all dependencies
44+
dep ensure -add github.com/pkg/errors add a dependency to the project
45+
46+
Use "dep help [command]" for more information about a command.
47+
48+
```
49+
50+
## 初始化
51+
在项目根目录执行初始化命令,`dep`在初始化时会分析应用程序所需要的所有依赖包,得出依赖包清单
52+
53+
并生成`vendor`目录,`Gopkg.toml``Gopkg.lock`文件
54+
55+
![image](https://golang.github.io/dep/docs/assets/func-toggles.png)
56+
57+
### 默认初始化
58+
```
59+
$ dep init -v
60+
```
61+
直接从对应网络资源处下载
62+
63+
### 优先从$GOPATH初始化
64+
```
65+
$ dep init -gopath -v
66+
```
67+
该命令会先从`$GOPATH`查找既有的依赖包,若不存在则从对应网络资源处下载
68+
69+
### Gopkg.toml
70+
71+
该文件由`dep init`生成,包含管理`dep`行为的规则声明
72+
73+
```
74+
required = ["github.com/user/thing/cmd/thing"]
75+
76+
ignored = [
77+
"github.com/user/project/pkgX",
78+
"bitbucket.org/user/project/pkgA/pkgY"
79+
]
80+
81+
[metadata]
82+
key1 = "value that convey data to other systems"
83+
system1-data = "value that is used by a system"
84+
system2-data = "value that is used by another system"
85+
86+
[[constraint]]
87+
# Required: the root import path of the project being constrained.
88+
name = "github.com/user/project"
89+
# Recommended: the version constraint to enforce for the project.
90+
# Note that only one of "branch", "version" or "revision" can be specified.
91+
version = "1.0.0"
92+
branch = "master"
93+
revision = "abc123"
94+
95+
# Optional: an alternate location (URL or import path) for the project's source.
96+
source = "https://github.com/myfork/package.git"
97+
98+
# Optional: metadata about the constraint or override that could be used by other independent systems
99+
[metadata]
100+
key1 = "value that convey data to other systems"
101+
system1-data = "value that is used by a system"
102+
system2-data = "value that is used by another system"
103+
```
104+
105+
### Gopkg.lock
106+
107+
该文件由`dep ensure``dep init`生成,包含一个项目依赖关系图的传递完整快照,表示为一系列`[[project]]`
108+
109+
```
110+
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
111+
112+
[[projects]]
113+
branch = "master"
114+
name = "github.com/golang/protobuf"
115+
packages = [
116+
"jsonpb",
117+
"proto",
118+
"protoc-gen-go/descriptor",
119+
"ptypes",
120+
"ptypes/any",
121+
"ptypes/duration",
122+
"ptypes/struct",
123+
"ptypes/timestamp"
124+
]
125+
revision = "bbd03ef6da3a115852eaf24c8a1c46aeb39aa175"
126+
```
127+
128+
## 常用命令
129+
130+
### dep ensure
131+
从项目中的`Gopkg.toml``Gopkg.lock`中分析关系图,并获取所需的依赖包
132+
133+
用于确保本地的关系图、锁、依赖包清单完全一致
134+
135+
### dep ensure -add
136+
137+
```
138+
# 引入该依赖包的最新版本
139+
dep ensure -add github.com/pkg/foo
140+
141+
# 引入具有特定约束(指定版本)的依赖包
142+
dep ensure -add github.com/pkg/foo@^1.0.1
143+
```
144+
145+
### dep ensure -update
146+
`Gopkg.lock`中的约定依赖项更新为`Gopkg.toml`允许的最新版本
147+
148+
## 最后
149+
150+
目前`dep`还在官方试验阶段,但已表示生产可安全使用
151+
152+
如果出现什么问题,大家可以一起留个言讨论讨论

0 commit comments

Comments
 (0)