«

go建立gin新项目报错了怎么处理

瑞瑞瑞 发布于 阅读:197


创建新的go项目,结果报错了..信息如下:

go: cannot determine module path for source directory D:\goframe\test (outside GOPATH, module path must be specified)

这个时候我们在命令的后面加一个项目名就可以了,比如 go mod init Test

现在安装gin框架:
D:\goframe\test>go get -u github.com/gin-gonic/gin
go: downloading github.com/gin-gonic/gin v1.10.0
go: downloading github.com/gin-contrib/sse v0.1.0
go: downloading golang.org/x/net v0.25.0
go: downloading github.com/go-playground/validator/v10 v10.20.0
go: downloading github.com/pelletier/go-toml/v2 v2.2.2
go: downloading github.com/ugorji/go/codec v1.2.12
go: downloading google.golang.org/protobuf v1.34.1
go: downloading github.com/go-playground/validator v9.31.0+incompatible
go: downloading github.com/go-playground/validator/v10 v10.22.0
go: downloading github.com/ugorji/go v1.2.12
go: downloading github.com/pelletier/go-toml v1.9.5
go: downloading github.com/bytedance/sonic v1.11.6
go: downloading github.com/goccy/go-json v0.10.2
go: downloading github.com/json-iterator/go v1.1.12
go: downloading golang.org/x/net v0.28.0
go: downloading google.golang.org/protobuf v1.34.2
go: downloading github.com/goccy/go-json v0.10.3
go: downloading golang.org/x/sys v0.20.0
go: downloading golang.org/x/sys v0.24.0
go: downloading github.com/gabriel-vasile/mimetype v1.4.3
go: downloading github.com/go-playground/universal-translator v0.18.1
go: downloading github.com/bytedance/sonic v1.12.1
go: downloading golang.org/x/crypto v0.23.0
go: downloading github.com/leodido/go-urn v1.4.0
go: downloading golang.org/x/text v0.15.0
go: downloading golang.org/x/crypto v0.26.0
go: downloading github.com/gabriel-vasile/mimetype v1.4.5
go: downloading golang.org/x/text v0.17.0
go: downloading github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: downloading github.com/modern-go/reflect2 v1.0.2
go: downloading github.com/go-playground/locales v0.14.1
go: downloading github.com/cloudwego/base64x v0.1.4
go: downloading golang.org/x/arch v0.8.0
go: downloading golang.org/x/arch v0.9.0
go: downloading github.com/bytedance/sonic/loader v0.1.1
go: downloading github.com/twitchyliquid64/golang-asm v0.15.1
go: downloading github.com/bytedance/sonic/loader v0.2.0
go: downloading github.com/klauspost/cpuid/v2 v2.2.7
go: downloading github.com/klauspost/cpuid v1.3.1
go: downloading github.com/klauspost/cpuid/v2 v2.2.8
go: downloading github.com/cloudwego/iasm v0.2.0
go: added github.com/bytedance/sonic v1.12.1
go: added github.com/bytedance/sonic/loader v0.2.0
go: added github.com/cloudwego/base64x v0.1.4
go: added github.com/cloudwego/iasm v0.2.0
go: added github.com/gabriel-vasile/mimetype v1.4.5
go: added github.com/gin-contrib/sse v0.1.0
go: added github.com/gin-gonic/gin v1.10.0
go: added github.com/go-playground/locales v0.14.1
go: added github.com/go-playground/universal-translator v0.18.1
go: added github.com/go-playground/validator/v10 v10.22.0
go: added github.com/goccy/go-json v0.10.3
go: added github.com/json-iterator/go v1.1.12
go: added github.com/klauspost/cpuid/v2 v2.2.8
go: added github.com/leodido/go-urn v1.4.0
go: added github.com/mattn/go-isatty v0.0.20
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.2.2
go: added github.com/twitchyliquid64/golang-asm v0.15.1
go: added github.com/ugorji/go/codec v1.2.12
go: added golang.org/x/arch v0.9.0
go: added golang.org/x/crypto v0.26.0
go: added golang.org/x/net v0.28.0
go: added golang.org/x/sys v0.24.0
go: added golang.org/x/text v0.17.0
go: added google.golang.org/protobuf v1.34.2
go: added gopkg.in/yaml.v3 v3.0.1

D:\goframe\test>

安装完成,这时我们写一个使用gin框架的go文件,文件名为ex.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() // 监听并在 0.0.0.0:8080 上启动服务
}

运行 go run ex.go,我们看到执行成功了!