rm -rf /home/music/go && tar -C /home/music -xzf go1.17.linux-amd64.tar.gz
验证是否安装成功
go version
进入 profile 文件
vim /etc/profile
在 profile 文件开头添加
export GOPATH=/home/music/go
export PATH=$GOPATH/bin:$PATH
重新加载系统配置文件
source /etc/profile
go install github.com/cloudwego/hertz/cmd/hz@latest
验证是否安装成功
hz -v
在 /home/music/go 目录外放置代码,例如 /home/music/project
在 /home/music/project 目录下创建 hertz_demo 文件夹
mkdir hertz_demo
hz new -mod hertz_demo
PS : 不加 -mod hertz_demo 会报错2
go mod tidy
go build -o hertz_demo && ./hertz_demo
如果成功启动,你将看到以下信息
2022/05/17 21:47:09.626332 engine.go:567: [Debug] HERTZ: Method=GET absolutePath=/ping --> handlerName=main.main.func1 (num=2 handlers)
2022/05/17 21:47:09.629874 transport.go:84: [Info] HERTZ: HTTP server listening on address=[::]:8888
此时不用操作
打开另一个窗口
curl http://127.0.0.1:8888/ping
如果不出意外,我们可以看到类似如下输出
$ {"message":"pong"}
到现在,我们已经成功启动了 Hertz Server,并完成了一次调用。
报错 # github.com/bytedance/sonic/internal/loader
…/…/go/pkg/mod/github.com/bytedance/sonic@v1.5.0/internal/loader/funcdata.go:27:22: undefined: _ModuleData
…/…/go/pkg/mod/github.com/bytedance/sonic@v1.5.0/internal/loader/funcdata.go:30:27: undefined: _ModuleData
…/…/go/pkg/mod/github.com/bytedance/sonic@v1.5.0/internal/loader/funcdata.go:41:6: undefined: _Func
…/…/go/pkg/mod/github.com/bytedance/sonic@v1.5.0/internal/loader/funcdata.go:42:12: undefined: _ModuleData
…/…/go/pkg/mod/github.com/bytedance/sonic@v1.5.0/internal/loader/funcdata.go:53:16: undefined: _ModuleData
…/…/go/pkg/mod/github.com/bytedance/sonic@v1.5.0/internal/loader/funcdata.go:79:26: undefined: _ModuleData
…/…/go/pkg/mod/github.com/bytedance/sonic@v1.5.0/internal/loader/funcdata_invalid.go:23:6: panic(“Unsupported Go version. Supported versions are: 1.15, 1.16, 1.17, 1.18, 1.19”) (no value) used as value
…/…/go/pkg/mod/github.com/bytedance/sonic@v1.5.0/internal/loader/loader.go:47:50: too many arguments in call to registerFunction
have (string, uintptr, uintptr, int, int, uintptr, uintptr, uintptr)
want (string, uintptr, int, int, uintptr) ↩︎
报错 output directory /home/cjz/workplace/hertz_demo is not under GOPATH/src.
Please specify a module name with the ‘-module’ flag ↩︎