有时我们并不在$GOPATH路径下创建项目。此时用Go Modules模式。实现此功能需要用到go mod命令和环境变量的配置。
1.go mod命令
2.go mod环境变量
1.go mod命令
2.go mod环境变量
说明:
#1.开始Go Modules模块
go env -w Go111MODULE=on #命令行
export Go111MODULE=on #修改 ~/.bashrc文件
vim ~/.bashrc
source ~/.bashrc
#2.初始化项目:
#2.1不要求在&GOPATH/src
mkdir -p $PATH/modules_test
#2.2创建一个模块名 生成文件go.mod(indirect是间接依赖的意思)
go mod init github.com/tang/moudles_test
# 名称:github.com/tang/moudles_test
3.在目录下创建文件:
main.go
4.go.sum :直接或间接依赖包的所有版本,今后项目依赖的版本不会被篡改。
main.go
package main
import(
"fmt"
"github.com/tang/zinx/ziface"
"github.com/tang/zinx/znet"
)
//ping test自定义路由
type PinRouter struct{
znet.BaseRouter
}
//ping Handle
func(this *PingRouter) Handle(request ziface.IRequest){
//先读取客户端的数据
fmt.Println("recv from client: msgId=",request.GetMsgID(),", data=",string(request.GetData()))
//再回写 ping
err := request.GetConnection().SendBuffMsg(0,[]byte["ping...ping...ping"])
if err !=nil{
fmt.Println(err)
}
}
func main(){
//1.创建一个server句柄
s:=znet.NewServer()
//2.配置路由
s.AddRouter(0,&PingRouter{})
//3.开启服务
s.Server()
}