记gin支持protobuf
By skyshappiness Posted 2022-06-04 13:13:04 In

环境:macos,gin,protobuf3


1、brew 安装 protobuf

2、go get -u github.com/golang/protobuf/protoc-gen-go

3、/opt/homebrew/opt/protobuf@3/bin/protoc --go_out=. user.proto


报错解决:

    1、protoc-gen-go: program not found or is not executable
        cd $GOPATH/go/bin && cp protoc-gen-go /usr/local/bin

     在 ~/.bash_profile 中添加:export GOPATH=$HOME/go PATH=$PATH:$GOPATH/bin
     source ~/.bash_profile. //重新初始化一下bash_profile

  2、protoc-gen-go: unable to determine Go import path for "user.proto"

     在文件中添加:option go_package="./";


附录:

server文件 : serve.go :

package main

import (

        "net/http"

        "github.com/gin-gonic/gin"

        module "./testdata"

)

func main() {

        r := gin.Default()

        // gin.H 是 map[string]interface{} 的一种快捷方式

        r.GET("/someJSON", func(c *gin.Context) {

                c.JSON(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})

        })

        r.GET("/moreJSON", func(c *gin.Context) {

                // 你也可以使用一个结构体

                var msg struct {

                        Name    string `json:"user"`

                        Message string

                        Number  int

                }

                msg.Name = "Lena"

                msg.Message = "hey"

                msg.Number = 123

                // 注意 msg.Name 在 JSON 中变成了 "user"

                // 将输出:{"user": "Lena", "Message": "hey", "Number": 123}

                c.JSON(http.StatusOK, msg)

        })

        r.GET("/someXML", func(c *gin.Context) {

                c.XML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})

        })

        r.GET("/someYAML", func(c *gin.Context) {

                c.YAML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})

        })

        r.GET("/protobuf", func(c *gin.Context) {

                data := &module.User{

                        Name: "张三",

                        Age:  20,

                }

                c.ProtoBuf(http.StatusOK, data)

        })

        // 监听并在 0.0.0.0:8080 上启动服务

        r.Run(":8080")

}


client.go:

package main

import (

        module "./testdata"

        "fmt"

        "github.com/golang/protobuf/proto"

        "io/ioutil"

        "net/http"

)

func main() {

        resp, err := http.Get("http://localhost:8080/protobuf")

        if err != nil {

                fmt.Println(err)

        } else {

                defer resp.Body.Close()

                body, err := ioutil.ReadAll(resp.Body)

                if err != nil {

                        fmt.Println(err)

                } else {

                        user := &module.User{}

                        proto.UnmarshalMerge(body, user)

                        fmt.Println(user)

                }

        }

}



protobuf文件:user.proto

syntax = "proto3";

option go_package="./";

package module;

// 定义数据结构,message 类似golang中的struct

message User {

  string name = 1;   // 定义一个string类型的字段name, 序号为1

  int32 age = 2;   // 定义一个int32类型的字段age, 序号为2

}



参考地址:

1、飞雪无情博客

2、李文周博客:https://www.liwenzhou.com/posts/Go/Protobuf3-language-guide-zh/

3、gin手册:https://gin-gonic.com/zh-cn/docs/examples/rendering/



友情链接
联系方式
  • 邮箱 / E-mail:121388038@qq.com