视频来源:B站《golang入门到项目实战 [2022最新Go语言教程,没有废话,纯干货!]》
文章为自己整理的学习笔记,侵权即删,谢谢支持!
main.go
中,去使用 utils.go
文件中的函数,如何实现——包包的本质实际上就是创建不同的文件夹,来存放程序文件。
go 的每一个文件都是属于一个包的,也就是说 go 是以包的形式来管理文件和项目目录结构的
包的三大作用:
语法:
打包基本语法:
package 包名
引入包基本语法:
import "包的路径"
我们将 func Cal
定义到文件 utils.go
, 将 utils.go
放到一个包中,当其它文件需要使用到 utils.go
的方法时,可以 import 该包
,就可以使用了.
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
在给一个文件打包时,该包对应一个文件夹,比如这里的 utils 文件夹对应的包名就是 utils,文件的包名通常和文件所在的文件夹名一致,一般为小写字母。
当一个文件要使用其它包函数或变量时,需要先引入对应的包。
为了让其它包的文件,可以访问到本包的函数,则该函数名的首字母需要大写,类似其它语言的 public ,这样才能跨包访问。
在访问其它包函数,变量时,其语法是 包名.函数名
如果包名较长,Go 支持给包取别名, 注意细节:取别名后,原来的包名就不能使用了
在同一包下,不能有相同的函数名(也不能有相同的全局变量名),否则报重复定义
Go modules是golang 1.11新加的特性,用来管理模块中包的依赖关系。
初始化模块
Go mod init <项目模块名称>
依赖关系处理,根据go.mod文件
Go mod tidy
将依赖包复制到项目的vendor目录
Go mod vendor
如果报被屏蔽(墙),可以使用这个命令,随后使用go build -mod=vendor
编译
显示依赖关系
Go list -m all
显示详细依赖关系
Go list -m -json all
下载依赖
Go mod download [path@version]
[path@version]是非必写的
目录结构:
打开终端,在go_pro文件夹下输入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
}
进入utils目录并执行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
终端执行 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_pro目录并执行 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"