运行gin官方样例

试一下gin框架,先尝试运行一下官方的样例

 

==下载gin框架==

执行go get报错如下

PS D:\work\10_Git\pulse-src> go get -u github.com/gin-gonic/gin
go: go.mod file not found in current directory or any parent directory.
        'go get' is no longer supported outside a module.
        To build and install a command, use 'go install' with a version,
        like 'go install example.com/cmd@latest'
        For more information, see https://golang.org/doc/go-get-install-deprecation
        or run 'go help get' or 'go help install'.
go: cannot find main module, but found .git/config in D:\work\10_Git\pulse-src
        to create a module there, run:
        go mod init

 

官方说字1.17之后,不应该用go get下载安装命令(即可执行程序),如果使用goget安装命令,就会提示上述警告,

也就是说go get只能下载普通的包,如果下载安装程序需要用go install

PS D:\work\10_Git\pulse-src> go install github.com/gin-gonic/gin@v1.7.7
go: downloading github.com/gin-gonic/gin v1.7.7
go: downloading github.com/gin-contrib/sse v0.1.0
go: downloading github.com/mattn/go-isatty v0.0.12
go: downloading github.com/golang/protobuf v1.3.3
go: downloading github.com/ugorji/go/codec v1.1.7
go: downloading gopkg.in/yaml.v2 v2.2.8
go: downloading github.com/go-playground/validator/v10 v10.4.1
go: downloading golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
go: downloading github.com/go-playground/universal-translator v0.17.0
go: downloading github.com/leodido/go-urn v1.2.0
go: downloading github.com/ugorji/go v1.1.7
go: downloading github.com/go-playground/locales v0.13.0
package github.com/gin-gonic/gin is not a main package
PS D:\work\10_Git\pulse-src>

 

==官方样例==

https://gin-gonic.com/zh-cn/docs/quickstart/

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运行代码报错

PS D:\work\10_Git\pulse-src> go run .\examples\gin.go
C:\Users\Chunhui.Qu\go\src\github.com\gin-gonic\gin\binding\protobuf.go:12:2: cannot find package "google.golang.org/protobuf/proto" in any of:
        C:\Program Files\Go\src\google.golang.org\protobuf\proto (from $GOROOT)
        C:\Users\Chunhui.Qu\go\src\google.golang.org\protobuf\proto (from $GOPATH)
PS D:\work\10_Git\pulse-src> 

 

解决办法

go get github.com/golang/protobuf/proto

重新执行go run命令,可以在控制台看到服务已经启动

 

 

==浏览器访问==

通过浏览器输入localhost:8000,可以看到打开页面。

 

posted @ 2022-03-31 17:58  大墨垂杨  阅读(413)  评论(0编辑  收藏  举报