(一)、Golang包
1.创建包
包可以区分命令空间 (一个文件夹不能有两个同名文件
),也可以更好的管理项目。Go中创建一个包,一般是创建一个文件夹。在该文件夹的go文件中,使用package
关键字声明包名称。通常文件夹名称和包名称相同,同一个文件下面只有一个包。
包名等于文件夹名
(1).创建一个包
声明man包,在main包下面我们需要实现一个main函数
同一个文件夹只能有声明一个包
package main func main() { println("test package") }
(2).导入包
要使用某一个包下面的变量与方法,我们需要导入这个包之后我们才能进行调用。导入包的注意事项: “要导入从GOPATH
开始的包路径”。列如,在service.go
中导入dao
包.
前提条件:
我们一定要有三个文件夹 src pkg bin
// 声明包名-有且只能声明一个 package service import "demo/dao" //导入包 // code 代码实现 func main() { }
(3).包注意事项
- 一个文件夹只能有一个package
- import后面的其实是GOPATH开始的相对路径,包括最后一段。但由于一个目录只能有一个package,所以 import 一个路径就等于是 import 了这个路径下的包。
- 一个package的文件不能在多个文件夹下
- 如果多个文件夹下面有重名的 package,他们其实是彼此无关的pacakage
- 如果一个go文件需要同时使用不同目录下的同名 pacakage, 需要在 import 这些目录时为每一个目录指定一个package的别名。
2.包管理工具 go module
(1).简介
go module是 golang1.11新添加的特性,用来管理模块中包的依赖关系
(2).go mod使用方法
- 初始化模块
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]
(3).实列演示
1. 首先打开我们的终端,我们需要对项目进行初始化的操作
go mod init 项目名
2.然后在service包下的userservice.go编写一个方法
user_service.go
// 声明包名-有且只能声明一个 package service import "fmt" // code 代码实现 func TestUseService() { fmt.Printf("test user service_use") }
3.在终端控制台进入我们被引入包的文件路径然后重构
cd service #进入包 go build 重构
PS C:\Environment\GoWorks\src\demo> go mod init demo go: C:\Environment\GoWorks\src\demo\go.mod already exists PS C:\Environment\GoWorks\src\demo> cd service #进入service包 PS C:\Environment\GoWorks\src\demo\service> go build #开始重构 PS C:\Environment\GoWorks\src\demo\service>
4.转入我们需要导入包的go文件中
main.go
package main // 导入demo目录下的service包进入这个go文件中 import "demo/service" func main() { println("test package") service.TestUseService() // 通过包名引用指定方法 }
5.如果我们此时还想在service包中添加方法,我们仍然需要go build
Customer_service.go
// 声明包名-有且只能声明一个 package service func TestCustomer() { println("test TestCustomer ...") }
我们需要在终端控制台的 service包下 继续 go build
go build
6.再次进行调用我们新添加的方法
package main // 导入demo目录下的service包进入这个go文件中 import ( "demo/service" ) func main() { println("test package") service.TestUseService() // 通过包名引用指定方法 service.TestCustomer() //再次通过包名调用 }
7.如果我们想要引入外部的包
进入官网的宝库: https://pkg.go.dev/
找到我么要导入包的命令,然后我们在终端进行输入这个命令
1. 假设我们引入 gin 这个包 go get -u github.com/gin-gonic/gin
然后在需要用的go文件引入包
使用这个包的一部分函数,否则在运行的时候编译器会自动给屏蔽掉
import "github.com/gin-gonic/gin"
package main // 导入demo目录下的service包进入这个go文件中 import ( "demo/service" "github.com/gin-gonic/gin" "net/http" ) func main() { println("test package") service.TestUseService() // 通过包名引用指定方法 service.TestCustomer() //再次通过包名调用 r := gin.Default() r.GET("/ping", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "message": "pong", }) }) r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") }
然后在终端退到我们的项目目录下执行
PS C:\Environment\GoWorks\src\demo\service> cd .. PS C:\Environment\GoWorks\src\demo> go mod tidy go: downloading github.com/stretchr/testify v1.8.2 go: downloading gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 go: downloading github.com/go-playground/assert/v2 v2.2.0 go: downloading github.com/davecgh/go-spew v1.1.1 go: downloading github.com/google/go-cmp v0.5.5 go: downloading golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 demo imports github.com/gin-gonic/gin tested by github.com/gin-gonic/gin.test imports github.com/stretchr/testify/assert: github.com/stretchr/testify@v1.8.2: read "https:/goproxy.cn/@v/v1.8.2.zip": read tcp 192.168.1.5:62970->222.35.78.41:443: wsarecv: A connect ion attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. demo imports github.com/gin-gonic/gin imports github.com/gin-gonic/gin/binding tested by github.com/gin-gonic/gin/binding.test imports github.com/stretchr/testify/require: github.com/stretchr/testify@v1.8.2: read "https:/goproxy.cn/@v/v1.8.2.zip": read tcp 192.168.1.5:62970->222.35.78.41:443: wsarecv: A connec tion attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. demo imports github.com/gin-gonic/gin imports github.com/gin-gonic/gin/binding imports github.com/go-playground/validator/v10 tested by github.com/go-playground/validator/v10.test imports github.com/go-playground/assert/v2: github.com/go-playground/assert/v2@v2.2.0: Get "https://goproxy.cn/github.com/go-playground/assert/v2/@v/v2.2.0.zip": read tcp 192.168.1.5:6 2970->222.35.78.41:443: wsarecv: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because conne cted host has failed to respond. demo imports github.com/gin-gonic/gin imports github.com/gin-gonic/gin/internal/json imports github.com/bytedance/sonic tested by github.com/bytedance/sonic.test imports github.com/davecgh/go-spew/spew: github.com/davecgh/go-spew@v1.1.1: Get "https://goproxy.cn/github.com/davecgh/go-spew/@v/v1.1.1.zip": read tcp 192.168.1.5:62970->222.35.78.41: 443: wsarecv: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has faile d to respond. demo imports github.com/gin-gonic/gin imports github.com/gin-gonic/gin/binding imports google.golang.org/protobuf/proto imports google.golang.org/protobuf/reflect/protoregistry tested by google.golang.org/protobuf/reflect/protoregistry.test imports github.com/google/go-cmp/cmp/cmpopts imports golang.org/x/xerrors: golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543: Get "https://goproxy.cn/golang.org/x/xerrors/@v/v0.0.0-20191204190536-9bdfabe68543.zip": read tcp 192.168.1.5:62970->222.35.78.41:443: wsarecv: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. PS C:\Environment\GoWorks\src\demo>
启动项目
(二)、Golang并发编程
1.并发编程之协程 go
Golang中的并发是函数相互独立运行的能力,Goroutines
是并发运行的函数。Golang提供了Goroutines作为并发处理操作的一种方式。
创建一个协程非常简单,就是在一个任务函数前面添加一个go关键字;
go task()
(1).没有添加协程的操作
time.Sleep(time.Millisecond * 500) //睡眠 100毫秒
没有添加协程的操作: 好像打印机一个个的输出
package main import ( "fmt" "time" ) func showMsg(msg string) { /*** 发现打印机的方式- 输入一个停顿一会会 */ for i := 0; i < len(msg); i++ { fmt.Printf("%c", msg[i]) time.Sleep(time.Millisecond * 100) //睡眠 100毫秒 } } func main() { showMsg("java") showMsg("golang") }
j->a->v->a->g->o->l->a->n->g
(2).添加协程的操作
添加完毕协程之后--
package main import ( "fmt" "time" ) func showMsg(msg string) { /*** 发现打印机的方式- 输入一个停顿一会会 */ for i := 0; i < len(msg); i++ { fmt.Printf("%c", msg[i]) time.Sleep(time.Millisecond * 500) //睡眠 100毫秒 } } func main() { go showMsg("java") // 开启一个go协程 showMsg("golang") }