Skip to content

Commit 88d3201

Browse files
committed
Update module example.
1 parent 806c1be commit 88d3201

File tree

5 files changed

+136
-18
lines changed

5 files changed

+136
-18
lines changed

example/module/README.md

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,70 @@
11

2+
## 初始化工程
23

34
```bash
4-
go list -m all
5-
go install
6-
go clean -modcache
7-
```
5+
mkdir mod2
6+
cd mod2
7+
go mod init mod2
8+
```
9+
10+
## 安装包
11+
12+
```bash
13+
go get -m github.com/gin-gonic/gin@v1.3.0
14+
15+
go get github.com/gorilla/mux # 匹配最新的一个 tag
16+
go get github.com/gorilla/mux@latest # 和上面一样
17+
go get github.com/gorilla/mux@v1.6.2 # 匹配 v1.6.2
18+
go get github.com/gorilla/mux@e3702bed2 # 匹配 v1.6.2
19+
go get github.com/gorilla/mux@c856192 # 匹配 c85619274f5d
20+
go get github.com/gorilla/mux@master # 匹配 master 分支
21+
```
22+
23+
1. 通过 `go get` 切换版本
24+
2. 执行 `go mod vendor` 将依赖复制到vendor下
25+
3. 通过 `go mod tidy` 命令可以移除 `go.mod` 中不再使用的依赖
26+
27+
## 设置代理
28+
29+
```bash
30+
# bash mac
31+
export GOPROXY=https://goproxy.io
32+
```
33+
34+
## mod 命令
35+
36+
```bash
37+
go help mod
38+
# Go mod provides access to operations on modules.
39+
40+
# Note that support for modules is built into all the go commands,
41+
# not just 'go mod'. For example, day-to-day adding, removing, upgrading,
42+
# and downgrading of dependencies should be done using 'go get'.
43+
# See 'go help modules' for an overview of module functionality.
44+
45+
Usage:
46+
47+
go mod <command> [arguments]
48+
49+
The commands are:
50+
51+
download download modules to local cache
52+
edit edit go.mod from tools or scripts
53+
graph print module requirement graph
54+
init initialize new module in current directory
55+
tidy add missing and remove unused modules
56+
vendor make vendored copy of dependencies
57+
verify verify dependencies have expected content
58+
why explain why packages or modules are needed
59+
60+
Use "go help mod <command>" for more information about a command.
61+
```
62+
63+
- `download`: download modules to local cache (下载依赖的module到本地cache)
64+
- `edit`: edit go.mod from tools or scripts (编辑go.mod文件)
65+
- `graph`: print module requirement graph (打印模块依赖图)
66+
- `init`: initialize new module in current directory (再当前文件夹下初始化一个新的module, 创建go.mod文件)
67+
- `tidy`: add missing and remove unused modules (增加丢失的module,去掉未用的module)
68+
- `vendor`: make vendored copy of dependencies (将依赖复制到vendor下)
69+
- `verify`: verify dependencies have expected content (校验依赖)
70+
- `why`: explain why packages or modules are needed (解释为什么需要依赖)

example/module/go.mod

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
module "https://github.com/gin-gonic/gin.git"
1+
module mod2
22

33
require (
44
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 // indirect
55
github.com/gin-gonic/gin v1.3.0
66
github.com/golang/protobuf v1.2.0 // indirect
7+
github.com/json-iterator/go v1.1.5 // indirect
78
github.com/mattn/go-isatty v0.0.4 // indirect
9+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
10+
github.com/modern-go/reflect2 v1.0.1 // indirect
11+
github.com/stretchr/testify v1.3.0 // indirect
812
github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 // indirect
13+
golang.org/x/net v0.0.0-20190110200230-915654e7eabc // indirect
14+
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 // indirect
15+
golang.org/x/sys v0.0.0-20190116161447-11f53e031339 // indirect
16+
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
917
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
1018
gopkg.in/yaml.v2 v2.2.2 // indirect
1119
)

example/module/go.sum

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cougNAV9szl6CVoj2RYwzS3DpUQNtlY=
24
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
35
github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
46
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
57
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
68
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
9+
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
10+
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
711
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
812
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
13+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
14+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
15+
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
16+
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
17+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
18+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
19+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
20+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
21+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
922
github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 h1:EICbibRW4JNKMcY+LsWmuwob+CRS1BmdRdjphAm9mH4=
1023
github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
24+
golang.org/x/net v0.0.0-20190110200230-915654e7eabc h1:Yx9JGxI1SBhVLFjpAkWMaO1TF+xyqtHLjZpvQboJGiM=
25+
golang.org/x/net v0.0.0-20190110200230-915654e7eabc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
26+
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
27+
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
28+
golang.org/x/sys v0.0.0-20190116161447-11f53e031339 h1:g/Jesu8+QLnA0CPzF3E1pURg0Byr7i6jLoX5sqjcAh0=
29+
golang.org/x/sys v0.0.0-20190116161447-11f53e031339/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
30+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1131
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
32+
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
33+
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
1234
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
1335
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
1436
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=

example/module/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
4+
import (
5+
"html/template"
6+
"log"
7+
"os"
8+
"github.com/gin-gonic/gin"
9+
)
10+
11+
var html = template.Must(template.New("https").Parse(`
12+
<html>
13+
<head>
14+
<title>Https Test</title>
15+
</head>
16+
<body>
17+
<h1 style="color:red;">Welcome, golang!</h1>
18+
</body>
19+
</html>
20+
`))
21+
22+
23+
func main() {
24+
logger := log.New(os.Stderr, "", 0)
25+
logger.Println("[WARNING] DON'T USE THE EMBED CERTS FROM THIS EXAMPLE IN PRODUCTION ENVIRONMENT, GENERATE YOUR OWN!")
26+
27+
r := gin.Default()
28+
r.SetHTMLTemplate(html)
29+
30+
r.GET("/", func(c *gin.Context) {
31+
c.HTML(200, "https", gin.H{
32+
"status": "success",
33+
})
34+
})
35+
36+
// Listen and Server in https://127.0.0.1:8080
37+
r.Run(":8083")
38+
}

example/module/module.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)