文章为自己整理的学习笔记,侵权即删,谢谢支持!
一 golang包1.1 包的引出
main.goutils.go
1.2 包的原理图
包的本质实际上就是创建不同的文件夹,来存放程序文件。
1.3 包的基本概念
go 的每一个文件都是属于一个包的,也就是说 go 是以包的形式来管理文件和项目目录结构的
包的三大作用:
- 区分相同名字的函数、变量等标识符
- 当程序文件很多时,可以很好的管理项目
- 控制函数、变量等访问范围,即作用域
语法:
package 包名
import "包的路径"
1.4 实例演示
func Calutils.goutils.goutils.goimport 该包
目录结构:
utils.go 文件:
package utils
import "fmt"
func Cal(n1 float64, n2 float64, operator byte) float64 {
var res float64
switch operator {
case '+':
res = n1 + n2
case '-':
res = n1 - n2
case '*':
res = n1 * n2
case '/':
res = n1 / n2
default:
fmt.Println("符号错误")
}
return res
}
main.go 文件:
package main
import (
"fmt"
"go_pro/utils"
)
func main() {
var n1 float64 = 1.2
var n2 float64 = 2.3
var operator byte = '+'
result := utils.Cal(n1, n2, operator)
fmt.Println("result = ", result)
}
运行结果:
[Running] go run "c:\Users\Mechrevo\Desktop\go_pro\main\main.go"
result = 3.5
1.5 注意事项
包名.函数名
二 golang包管理工具go module
Go modules是golang 1.11新加的特性,用来管理模块中包的依赖关系。
2.1 Go mod使用方法
Go mod init <项目模块名称>
Go mod tidy
Go mod vendor
Go list -m all
Go list -m -json all
Go mod download [path@version]
2.2 自定义包实例演示
目录结构:
go mode init go_pro
PS C:\Users\Mechrevo\Desktop\go_pro> go mod init go_pro
go: creating new go.mod: module go_pro
go: to add module requirements and sums:
go mod tidy
自动生成了一个go.mod文件,内容如下:
Run go mod tidy
module go_pro
go 1.18
编辑utils目录下utils.go文件:
package utils
import "fmt"
func Cal(n1 float64, n2 float64, operator byte) float64 {
var res float64
switch operator {
case '+':
res = n1 + n2
case '-':
res = n1 - n2
case '*':
res = n1 * n2
case '/':
res = n1 / n2
default:
fmt.Println("符号错误")
}
return res
}
go build
PS C:\Users\Mechrevo\Desktop\go_pro> cd .\utils\
PS C:\Users\Mechrevo\Desktop\go_pro\utils> go build
在main.go中导入utils包:
package main
import (
"fmt"
"go_pro/utils" // 引入包
)
func main() {
var n1 float64 = 1.2
var n2 float64 = 2.3
var operator byte = '+'
result := utils.Cal(n1, n2, operator) // 使用导入的包内函数
fmt.Println("result = ", result)
}
运行结果:
[Running] go run "c:\Users\Mechrevo\Desktop\go_pro\main\main.go"
result = 3.5
2.3 导入远程包
终端执行 go get -u github.com/gin-gonic/gin 下载
PS C:\Users\Mechrevo\Desktop\go_pro\utils> go get -u github.com/gin-gonic/gin
go: downloading github.com/gin-gonic/gin v1.8.1
go: downloading github.com/gin-contrib/sse v0.1.0
go: downloading github.com/mattn/go-isatty v0.0.14
go: downloading golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
go: downloading github.com/goccy/go-json v0.9.7
go: downloading github.com/json-iterator/go v1.1.12
go: downloading github.com/pelletier/go-toml/v2 v2.0.1
go: downloading github.com/ugorji/go/codec v1.2.7
go: downloading google.golang.org/protobuf v1.28.0
go: downloading github.com/go-playground/validator/v10 v10.10.0
go: downloading golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069
go: downloading github.com/ugorji/go v1.2.7
go: downloading github.com/go-playground/validator v9.31.0+incompatible
go: downloading github.com/go-playground/validator/v10 v10.11.0
go: downloading github.com/pelletier/go-toml v1.9.5
go: downloading github.com/pelletier/go-toml/v2 v2.0.2
go: downloading github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
go: downloading github.com/modern-go/reflect2 v1.0.2
go: downloading github.com/go-playground/universal-translator v0.18.0
go: downloading github.com/leodido/go-urn v1.2.1
go: downloading golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97
go: downloading golang.org/x/text v0.3.6
go: downloading github.com/go-playground/locales v0.14.0
go: downloading github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: downloading golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e
go: downloading golang.org/x/sys v0.0.0-20220624220833-87e55d714810
go: downloading golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d
go: added github.com/gin-contrib/sse v0.1.0
go: added github.com/gin-gonic/gin v1.8.1
go: added github.com/go-playground/locales v0.14.0
go: added github.com/go-playground/universal-translator v0.18.0
go: added github.com/go-playground/validator/v10 v10.11.0
go: added github.com/goccy/go-json v0.9.7
go: added github.com/json-iterator/go v1.1.12
go: added github.com/leodido/go-urn v1.2.1
go: added github.com/mattn/go-isatty v0.0.14
go: added github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: added github.com/modern-go/reflect2 v1.0.2
go: added github.com/pelletier/go-toml/v2 v2.0.2
go: added github.com/ugorji/go/codec v1.2.7
go: added golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d
go: added golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e
go: added golang.org/x/sys v0.0.0-20220624220833-87e55d714810
go: added golang.org/x/text v0.3.7
go: added google.golang.org/protobuf v1.28.0
go: added gopkg.in/yaml.v2 v2.4.0
会看到go.mod文件内添加了很多内容:
module go_pro
go 1.18
require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.8.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
导入远程包并编辑main.go文件:
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
go mod tidy
PS C:\Users\Mechrevo\Desktop\go_pro\utils> cd ..
PS C:\Users\Mechrevo\Desktop\go_pro> go mod tidy
go: downloading github.com/stretchr/testify v1.7.2
go: downloading github.com/google/go-cmp v0.5.5
go: downloading gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
go: downloading github.com/go-playground/assert/v2 v2.0.1
go: downloading github.com/kr/pretty v0.3.0
go: downloading github.com/kr/text v0.2.0
go: downloading github.com/rogpeppe/go-internal v1.8.0
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading gopkg.in/yaml.v3 v3.0.1
执行main.go文件后访问localhost:8080/ping:
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /ping --> main.main.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2022/06/27 - 12:22:25 | 200 | 0s | 127.0.0.1 | GET "/ping"