Skip to content

Commit 5308dba

Browse files
committed
update
1 parent 4af359f commit 5308dba

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed

docs/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,45 @@ $ go env GOPATH
172172
$ go env -w GOPROXY=https://goproxy.cn,direct
173173
```
174174

175+
关于 GOPATH:
176+
177+
The `GOPATH` environment variable specifies the location of your workspace.
178+
179+
It defaults to a directory named `go`inside your home directory, so `$HOME/go` on Unix, `$home/go` on Plan 9, and `%USERPROFILE%\go` (usually `C:\Users\YourName\go`) on Windows.
180+
181+
182+
```sh
183+
$ tree -L 3 ~/go
184+
/Users/wangtom/go
185+
├── bin
186+
│   ├── go-outline
187+
│   ├── goimports
188+
│   └── gopls
189+
├── pkg
190+
│   ├── darwin_amd64
191+
│   │   └── github.com
192+
│   ├── mod
193+
│   │   ├── cache
194+
│   │   ├── github.com
195+
│   │   ├── golang.org
196+
│   │   ├── google.golang.org
197+
│   │   ├── gopkg.in
198+
│   │   └── honnef.co
199+
│   └── sumdb
200+
│   └── sum.golang.org
201+
└── src
202+
└── github.com
203+
└── go-sql-driver
204+
```
205+
206+
175207
## 创建 go modules 模块:
176208

209+
我们知道 `go get` 获取的代码会放在 `$GOPATH/src` 下面,而`go build`会在`$GOROOT/src``$GOPATH/src`下面按照import path去搜索`package`
210+
211+
每个go.mod文件定义了一个module,而放置go.mod文件的目录被称为module root目录(通常对应一个repo的root目录,但不是必须的)。module root目录以及其子目录下的所有Go package均归属于该module,除了那些自身包含go.mod文件的子目录。
212+
213+
177214
大多数编程语言都会有包管理工具,像Node有`npm`,PHP有`composer`,Java有`Maven``Gradle`
178215
可是,Go语言一直缺乏一个官方的包管理(曾有一个`Dep`被称为官方试验品`official experiment`)。
179216
终于,在`go1.11` 版本中,新增了`module`管理模块功能,用来管理依赖包。

examples/go-mod-demo/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
APP_NAME=Go-Mod-DEMO
2+
APP_DEBUG=true
3+
APP_VERSION=0.0.1

examples/go-mod-demo/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
README.md
2+
3+
4+
```
5+
$ mkdir go-mod-demo
6+
$ cd go-mod-demo
7+
8+
$ ll
9+
total 16
10+
-rw-r--r-- 1 wangtom staff 9B 12 2 11:09 README.md
11+
-rw-r--r-- 1 wangtom staff 192B 12 2 11:08 main.go
12+
```
13+
14+
15+
$ go mod init go-mod-demo
16+
go: creating new go.mod: module go-mod-demo
17+
18+
```
19+
$ cat go.mod
20+
module go-mod-demo
21+
22+
go 1.13
23+
```
24+
25+
26+
go list -m输出的信息被称为build list,也就是构建当前module所要构建的所有相关package(及版本)的列表。
27+
28+
```
29+
$ go list -m -json all
30+
{
31+
"Path": "go-mod-demo",
32+
"Main": true,
33+
"Dir": "/Users/wangtom/Code/golang-quick-start/examples/go-mod-demo",
34+
"GoMod": "/Users/wangtom/Code/golang-quick-start/examples/go-mod-demo/go.mod",
35+
"GoVersion": "1.13"
36+
}
37+
```
38+
39+
40+
```sh
41+
$ go get -u github.com/joho/godotenv
42+
$ ll
43+
total 32
44+
-rw-r--r-- 1 wangtom staff 695B 12 2 11:19 README.md
45+
-rw-r--r-- 1 wangtom staff 81B 12 2 11:21 go.mod
46+
-rw-r--r-- 1 wangtom staff 167B 12 2 11:21 go.sum
47+
-rw-r--r-- 1 wangtom staff 192B 12 2 11:08 main.go
48+
```
49+
50+
51+
```sh
52+
$ cat go.mod
53+
module go-mod-demo
54+
55+
go 1.13
56+
57+
require github.com/joho/godotenv v1.3.0 // indirect
58+
```
59+
➜ go-mod-demo git:(master) ✗
60+
61+
62+
使用 go mod 的依赖包会被安装在 `~/go/pkg/mod/` 命令下:
63+
64+
```sh
65+
$ ll ~/go/pkg/mod/github.com/joho/godotenv@v1.3.0/
66+
total 80
67+
-r--r--r-- 1 wangtom staff 1.0K 12 2 09:40 LICENCE
68+
-r--r--r-- 1 wangtom staff 4.8K 12 2 09:40 README.md
69+
dr-xr-xr-x 3 wangtom staff 96B 12 2 09:40 autoload
70+
dr-xr-xr-x 3 wangtom staff 96B 12 2 09:40 cmd
71+
dr-xr-xr-x 8 wangtom staff 256B 12 2 09:40 fixtures
72+
-r--r--r-- 1 wangtom staff 8.6K 12 2 09:40 godotenv.go
73+
-r--r--r-- 1 wangtom staff 12K 12 2 09:40 godotenv_test.go
74+
```
75+
76+
77+
78+
vi .env
79+
80+
APP_NAME=Go-Mod-DEMO
81+
APP_DEBUG=true
82+
APP_VERSION=0.0.1
83+
84+

examples/go-mod-demo/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module go-mod-demo
2+
3+
go 1.13
4+
5+
require github.com/joho/godotenv v1.3.0 // indirect

examples/go-mod-demo/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
2+
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=

examples/go-mod-demo/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
"time"
7+
8+
"github.com/joho/godotenv"
9+
)
10+
11+
func main() {
12+
13+
log.Println("Hello, World!")
14+
15+
log.Printf("Unix timestamp: %d \n", time.Now().Unix())
16+
17+
err := godotenv.Load()
18+
if err != nil {
19+
20+
log.Fatal("Error loading .env file")
21+
}
22+
23+
appName := os.Getenv("APP_NAME")
24+
isDebug := os.Getenv("APP_DEBUG")
25+
26+
log.Println("APP_NAME: ", appName)
27+
log.Println("APP_DEBUGL: ", isDebug)
28+
29+
}

0 commit comments

Comments
 (0)