从不同的包导入 proto 文件会导致“缺少方法 protoreflect”

2023-11-21

我对 Go 和 Protobufs 很陌生,因此这可能是一个非常菜鸟的问题。对此感到抱歉。

我的 go 项目中有几个包,我想创建一个单独的包,其中包含我所有的 .proto (也是 .pb.go)文件,然后我可以将这些 proto 文件导入到任何其他包中,以更好地管理我的所有 proto 文件。

但是,当我将原始文件移动到一个名为“proto temps”的单独包并在另一个名为“reader”的包中导入“proto temps”时。在 reader.go 中,我这样做:

    sensorData := &prototemps.Sensor{}
    err := proto.Unmarshal(msg.Payload(), sensorData) 

它产生这个错误

var sensorData *prototemps.Sensor
cannot use sensorData (variable of type *prototemps.Sensor) as protoreflect.ProtoMessage value in argument to proto.Unmarshal: missing method ProtoReflect

我的项目结构如下:

ProjectFolder/
 /prototemps/<all .proto and .pb.go exist here>  (Package "prototemps")
 /reader/reader.go which fails when tries to do proto.Unmarshall (Package "reader")

这是我的 .proto 的样子

syntax="proto3";
package prototemps;

import "google/protobuf/timestamp.proto";

message sensor {
      string Name = 1;
      int32 ID = 2;
      string Type = 3;
      orientation Ori = 4;
      IO IO = 5;
      google.protobuf.Timestamp ts = 6;
}

message orientation {
      int32 X = 1;
      int32 Y = 2;
      int32 Z = 3;
}

message IO {
      int32 Reads = 1;
      int32 Writes = 2;
}

这是我使用 * 生成的 .pb.go协议 --go_out=..proto

// Code generated by protoc-gen-go. DO NOT EDIT.
// source: sensorData.proto

package prototemps

import (
    fmt "fmt"
    proto "github.com/golang/protobuf/proto"
    timestamppb "google.golang.org/protobuf/types/known/timestamppb"
    math "math"
)

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package

type Sensor struct {
    Name                 string                 `protobuf:"bytes,1,opt,name=Name,proto3" json:"Name,omitempty"`
    ID                   int32                  `protobuf:"varint,2,opt,name=ID,proto3" json:"ID,omitempty"`
    Type                 string                 `protobuf:"bytes,3,opt,name=Type,proto3" json:"Type,omitempty"`
    Ori                  *Orientation           `protobuf:"bytes,4,opt,name=Ori,proto3" json:"Ori,omitempty"`
    IO                   *IO                    `protobuf:"bytes,5,opt,name=IO,proto3" json:"IO,omitempty"`
    Ts                   *timestamppb.Timestamp `protobuf:"bytes,6,opt,name=ts,proto3" json:"ts,omitempty"`
    XXX_NoUnkeyedLiteral struct{}               `json:"-"`
    XXX_unrecognized     []byte                 `json:"-"`
    XXX_sizecache        int32                  `json:"-"`
}

func (m *Sensor) Reset()         { *m = Sensor{} }
func (m *Sensor) String() string { return proto.CompactTextString(m) }
func (*Sensor) ProtoMessage()    {}
func (*Sensor) Descriptor() ([]byte, []int) {
    return fileDescriptor_a3adf506f94bdd26, []int{0}
}

func (m *Sensor) XXX_Unmarshal(b []byte) error {
    return xxx_messageInfo_Sensor.Unmarshal(m, b)
}
func (m *Sensor) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
    return xxx_messageInfo_Sensor.Marshal(b, m, deterministic)
}
func (m *Sensor) XXX_Merge(src proto.Message) {
    xxx_messageInfo_Sensor.Merge(m, src)
}
func (m *Sensor) XXX_Size() int {
    return xxx_messageInfo_Sensor.Size(m)
}
func (m *Sensor) XXX_DiscardUnknown() {
    xxx_messageInfo_Sensor.DiscardUnknown(m)
}

var xxx_messageInfo_Sensor proto.InternalMessageInfo

func (m *Sensor) GetName() string {
    if m != nil {
        return m.Name
    }
    return ""
}

func (m *Sensor) GetID() int32 {
    if m != nil {
        return m.ID
    }
    return 0
}

func (m *Sensor) GetType() string {
    if m != nil {
        return m.Type
    }
    return ""
}

func (m *Sensor) GetOri() *Orientation {
    if m != nil {
        return m.Ori
    }
    return nil
}

func (m *Sensor) GetIO() *IO {
    if m != nil {
        return m.IO
    }
    return nil
}

func (m *Sensor) GetTs() *timestamppb.Timestamp {
    if m != nil {
        return m.Ts
    }
    return nil
}

type Orientation struct {
    X                    int32    `protobuf:"varint,1,opt,name=X,proto3" json:"X,omitempty"`
    Y                    int32    `protobuf:"varint,2,opt,name=Y,proto3" json:"Y,omitempty"`
    Z                    int32    `protobuf:"varint,3,opt,name=Z,proto3" json:"Z,omitempty"`
    XXX_NoUnkeyedLiteral struct{} `json:"-"`
    XXX_unrecognized     []byte   `json:"-"`
    XXX_sizecache        int32    `json:"-"`
}

func (m *Orientation) Reset()         { *m = Orientation{} }
func (m *Orientation) String() string { return proto.CompactTextString(m) }
func (*Orientation) ProtoMessage()    {}
func (*Orientation) Descriptor() ([]byte, []int) {
    return fileDescriptor_a3adf506f94bdd26, []int{1}
}

func (m *Orientation) XXX_Unmarshal(b []byte) error {
    return xxx_messageInfo_Orientation.Unmarshal(m, b)
}
func (m *Orientation) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
    return xxx_messageInfo_Orientation.Marshal(b, m, deterministic)
}
func (m *Orientation) XXX_Merge(src proto.Message) {
    xxx_messageInfo_Orientation.Merge(m, src)
}
func (m *Orientation) XXX_Size() int {
    return xxx_messageInfo_Orientation.Size(m)
}
func (m *Orientation) XXX_DiscardUnknown() {
    xxx_messageInfo_Orientation.DiscardUnknown(m)
}

var xxx_messageInfo_Orientation proto.InternalMessageInfo

func (m *Orientation) GetX() int32 {
    if m != nil {
        return m.X
    }
    return 0
}

func (m *Orientation) GetY() int32 {
    if m != nil {
        return m.Y
    }
    return 0
}

func (m *Orientation) GetZ() int32 {
    if m != nil {
        return m.Z
    }
    return 0
}

type IO struct {
    Reads                int32    `protobuf:"varint,1,opt,name=Reads,proto3" json:"Reads,omitempty"`
    Writes               int32    `protobuf:"varint,2,opt,name=Writes,proto3" json:"Writes,omitempty"`
    XXX_NoUnkeyedLiteral struct{} `json:"-"`
    XXX_unrecognized     []byte   `json:"-"`
    XXX_sizecache        int32    `json:"-"`
}

func (m *IO) Reset()         { *m = IO{} }
func (m *IO) String() string { return proto.CompactTextString(m) }
func (*IO) ProtoMessage()    {}
func (*IO) Descriptor() ([]byte, []int) {
    return fileDescriptor_a3adf506f94bdd26, []int{2}
}

func (m *IO) XXX_Unmarshal(b []byte) error {
    return xxx_messageInfo_IO.Unmarshal(m, b)
}
func (m *IO) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
    return xxx_messageInfo_IO.Marshal(b, m, deterministic)
}
func (m *IO) XXX_Merge(src proto.Message) {
    xxx_messageInfo_IO.Merge(m, src)
}
func (m *IO) XXX_Size() int {
    return xxx_messageInfo_IO.Size(m)
}
func (m *IO) XXX_DiscardUnknown() {
    xxx_messageInfo_IO.DiscardUnknown(m)
}

var xxx_messageInfo_IO proto.InternalMessageInfo

func (m *IO) GetReads() int32 {
    if m != nil {
        return m.Reads
    }
    return 0
}

func (m *IO) GetWrites() int32 {
    if m != nil {
        return m.Writes
    }
    return 0
}

func init() {
    proto.RegisterType((*Sensor)(nil), "prototemps.sensor")
    proto.RegisterType((*Orientation)(nil), "prototemps.orientation")
    proto.RegisterType((*IO)(nil), "prototemps.IO")
}

func init() {
    proto.RegisterFile("sensorData.proto", fileDescriptor_a3adf506f94bdd26)
}

var fileDescriptor_a3adf506f94bdd26 = []byte{
    // 259 bytes of a gzipped FileDescriptorProto
    0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x4c, 0x8e, 0xcf, 0x4a, 0xc3, 0x40,
    0x10, 0xc6, 0xd9, 0xb4, 0x09, 0x38, 0x95, 0x22, 0x83, 0xe8, 0xd2, 0x83, 0x4a, 0x4e, 0xea, 0x61,
    0x0b, 0xf5, 0xe0, 0x0b, 0xf4, 0xd2, 0x8b, 0x81, 0xa5, 0x60, 0xdb, 0xdb, 0x16, 0xd7, 0xb2, 0x60,
    0xb2, 0x61, 0x77, 0x3c, 0xf8, 0x64, 0xbe, 0x9e, 0xfb, 0x27, 0xc5, 0x9c, 0x32, 0xdf, 0xe4, 0x37,
    0xdf, 0xfe, 0xe0, 0xca, 0xeb, 0xce, 0x5b, 0xb7, 0x56, 0xa4, 0x44, 0xef, 0x2c, 0x59, 0x84, 0xf4,
    0x21, 0xdd, 0xf6, 0x7e, 0x71, 0x7f, 0xb2, 0xf6, 0xf4, 0xa5, 0x97, 0x69, 0x75, 0xfc, 0xfe, 0x5c,
    0x92, 0x69, 0xb5, 0x27, 0xd5, 0xf6, 0x19, 0xae, 0x7f, 0x19, 0x54, 0xb9, 0x01, 0x11, 0xa6, 0x6f,
    0xaa, 0xd5, 0x9c, 0x3d, 0xb0, 0xc7, 0x0b, 0x99, 0x66, 0x9c, 0x43, 0xb1, 0x59, 0xf3, 0x22, 0x6c,
    0x4a, 0x19, 0xa6, 0xc8, 0x6c, 0x7f, 0x7a, 0xcd, 0x27, 0x99, 0x89, 0x33, 0x3e, 0xc1, 0xa4, 0x71,
    0x86, 0x4f, 0xc3, 0x6a, 0xb6, 0xba, 0x15, 0xff, 0xaf, 0x0b, 0xeb, 0x8c, 0xee, 0x48, 0x91, 0xb1,
    0x9d, 0x8c, 0x0c, 0xde, 0x85, 0xba, 0x86, 0x97, 0x89, 0x9c, 0x8f, 0xc9, 0x4d, 0x13, 0xea, 0x1b,
    0x7c, 0x86, 0x82, 0x3c, 0xaf, 0xd2, 0xff, 0x85, 0xc8, 0xee, 0xe2, 0xec, 0x2e, 0xb6, 0x67, 0x77,
    0x19, 0xa8, 0xfa, 0x15, 0x66, 0xa3, 0x7e, 0xbc, 0x04, 0xb6, 0x4b, 0xea, 0xa5, 0x64, 0xbb, 0x98,
    0xf6, 0x83, 0x36, 0xdb, 0xc7, 0x74, 0x48, 0xca, 0x21, 0x1d, 0xea, 0x55, 0x94, 0xc0, 0x6b, 0x28,
    0xa5, 0x56, 0x1f, 0x7e, 0xb8, 0xc9, 0x01, 0x6f, 0xa0, 0x7a, 0x77, 0x86, 0xb4, 0x1f, 0x8e, 0x87,
    0x74, 0xac, 0x92, 0xc4, 0xcb, 0x5f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xa3, 0x7e, 0xde, 0x40, 0x6e,
    0x01, 0x00, 0x00,
}

任何帮助将不胜感激,谢谢!


如果您对 protoc-gen-go 版本感到满意,您可能只想更改您的go进口地点proto.Unmarshal叫做。我知道有两种选择:

  • "github.com/golang/protobuf/proto"

  • "google.golang.org/protobuf/proto"

尝试一下你没有的那个。希望对其他人也有帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从不同的包导入 proto 文件会导致“缺少方法 protoreflect” 的相关文章

  • Golang 中的“相互”包导入

    是否可以在 Golang 中执行 相互 包导入之类的操作 举例来说 我有两个包 A 和 B 分别具有 AFunc 和 BFunc BFunc2 函数 package A import B func AFunc do stuff but al
  • 为什么我不能执行 fmt.Sprintf("%d.%d.%d.%d", a...)?

    我正在学习 Go 但我一直坚持 Go 之旅 exercise stringer go https tour golang org methods 7 https tour golang org methods 7 这是一些代码 type I
  • Golang delve,如何启动调试器并启动正在调试的应用程序?

    我正在尝试设置一个可以远程连接的无头深度调试器 我无法找到一种方法来启动调试服务器 而该服务器不会暂停我正在调试的应用程序 我一直在使用dlv attach headless true listen 2345 attach 32但这会暂停该
  • 在 Go 中修改导入的库

    我的问题 弹性节拍 https www elastic co products beats是一个用 Go 编写的日志传送程序的开源项目 它具有多种日志输出功能 包括控制台 Elasticsearch 和 Redis 我想将我自己的输出添加到
  • Go 中数组的嵌套结构

    我已经开始使用https mholt github io json to go https mholt github io json to go 将 API JSON 转换为 go 结构 我真的很喜欢它 但是我对如何初始化如下所示的报告定义
  • gRPC + 图片上传

    我想创建一个简单的gRPC用户可以上传他 她的图片的端点 协议缓冲区声明如下 message UploadImageRequest AuthToken auth 1 An enum with either JPG or PNG FileTy
  • Google Cloud Kubernetes 上任务队列的替代方案

    我发现任务队列主要用于App Engine标准环境 我正在将现有服务从 App Engine 迁移到 Kubernetes 任务队列的一个好的替代方案是什么 推送队列是当前正在使用的队列 我在线阅读文档并浏览了此链接 何时使用 PubSub
  • ReverseProxy取决于golang中的request.Body

    我想构建一个 http 反向代理 它检查 HTTP 主体 然后将 HTTP 请求发送到它的上游服务器 你怎么能在 Go 中做到这一点 初始尝试 如下 失败 因为 ReverseProxy 复制传入请求 修改它并发送 但正文已被读取 func
  • 使用cgo时的多重定义

    package main int add int a int b return a b import C import fmt func main func Test1 fmt Println C add 1 3 export Test2
  • 有没有一种方法可以在不停机的情况下更新 net/http 服务器中的 TLS 证书?

    我有一个简单的 https 服务器 提供一个简单的页面 如下所示 为简洁起见 没有错误处理 package main import crypto tls fmt net http func main mux http NewServeMux
  • Golang标志:忽略丢失的标志并解析多个重复的标志

    我是 Golang 新手 一直无法使用 flag 找到这个问题的解决方案 如何使用 flag 以便我的程序可以处理此类调用 其中 term 标志可能出现可变次数 包括 0 次 myprogram f flag1 myprogram f fl
  • 如何使用 Java 原生接口从 Java 调用 Go 函数?

    可以通过以下方式调用 C 方法JNA https en wikipedia org wiki Java Native AccessJava 中的接口 如何使用 Go 实现相同的功能 package main import fmt impor
  • 在 Go 中使用互斥锁

    我想了解互斥体是如何工作的 据我目前的理解 它是为了进行原子操作并同步对某些数据的访问 我在这里构建了一个队列数据结构的示例 https github com arnauddri algorithms blob master data st
  • Golang 优雅地关闭 HTTP 服务器并进行错误处理

    我正在让我的 HTTP 服务器正常关闭 我从帖子中获取了提示here https stackoverflow com questions 39320025 how to stop http listenandserve 并且到目前为止已经像
  • Go中如何从json字符串中获取键值

    我想尝试从 Go 中的 JSON 获取键值 但我不确定如何操作 我已经能够使用 simplejson 读取 json 值 但是我无法找到如何获取键值 有人能指出我正确的方向和 或帮助我吗 谢谢你 您可以通过执行以下操作来获取 JSON 结构
  • 构建链代码时 ltdl.h 未找到错误

    我正在尝试使用构建链码go build 当我运行 Go build 命令时它的报告 hyperledger fabric vendor github com miekg pkcs11 pkcs11 g o 29 18 fatal error
  • 当涉及多个渠道时,select 如何工作?

    我发现在多个非缓冲通道上使用 select 时 例如 select case lt chana case lt chanb 即使两个通道都有数据 但在处理此选择时 case chana 和 case chanb 的跟注不平衡 package
  • GAE Go — 如何对不存在的实体键使用 GetMulti?

    我发现自己需要做一个GetMulti使用键数组进行操作 其中某些实体存在 但有些实体不存在 我当前的代码 如下 返回错误 datastore no such entity err datastore GetMulti c keys info
  • GoQt 致命错误:QAbstractAnimation:没有这样的文件或目录

    我尝试编译 Qt 来开发桌面应用程序 我按照 Qt 网站上的官方 wiki 指南的说明进行操作 当我尝试go run示例文件夹中的示例 我收到错误 去运行 home pinkya rabbit workspace go1programs s
  • 解组转义 XML

    在 Go 中 我将如何解码此 XML 响应 我尝试过建立一个自定义UnMarshal方法在我的Answerstruct 但我运气不太好

随机推荐

  • Scala:泛型类型的方法重载

    在 C 中 我可以重载泛型类型的方法 如下例所示 http ideone com QVooD using System using System Collections Generic public class Test public st
  • TensorFlow 中的步骤和纪元有什么区别?

    在大多数模型中 都有一个steps参数表示运行数据的步骤数 但我发现在大多数实际使用中 我们还执行拟合函数 Nepochs 运行 1000 步 1 epoch 和运行 100 步 10 epoch 有什么区别 实践中哪一种更好 连续的纪元之
  • 如何在C#中清除Cursor.Clip并允许光标再次自由移动?

    我正在尝试将光标锁定到表单中 这是针对鼠标储物柜应用程序的 我正在尝试处置光标 以便它将重置Cursor Clip当他们解锁它时 到目前为止我有 Cursor Clip new Rectangle x 8 y 30 Size Width 1
  • 如何判断一个字符串是英语还是波斯语?

    我在表单中有 edittext 我希望当用户将文本输入到 edittext 中时 我的程序可以检测到 edittext 中插入了哪种语言 有没有办法确定字符串是英语还是波斯语 我找到了阿拉伯语的代码 public static boolea
  • 将日期截断为会计年度

    以下数据库视图将日期截断为会计年度 4 月 1 日 CREATE OR REPLACE VIEW FISCAL YEAR VW AS SELECT CASE WHEN to number to char SYSDATE MM lt 4 TH
  • eclipse,新的android应用程序项目,“下一步按钮”在需要项目名称的部分被禁用

    我想开始为android编程 所以我下载了Eclipse并在其上安装了ADT插件并下载了Android SDK 现在 当我想在 文件 gt 新建 gt Android应用程序项目 中创建一个新的android项目时 它会将我引导到一个需要
  • 在 Python 中求解线性整数方程组

    我正在寻找一种用 Python 求解线性方程组的方法 特别是 我正在寻找大于全零的最小整数向量并求解给定的方程 例如 我有以下等式 and want to solve In this case the smallest integer ve
  • 如何为 UIWebView 实现 NTLM 身份验证?

    我有一个用例UIWebView可能需要连接受 NTLM 保护的 Web 服务器 我还有一个用例 我已经拥有要传递的凭据 因此 我不强迫用户输入它们 而是如何与UIWebView UPDATE 使用此方法here当您执行简单的 GET 请求时
  • 使用 JavaScript 将文本截断为特定大小 (8 KB)

    我正在使用ZemantaAPI 每次调用最多接受 8 KB 的文本 我正在使用 JavaScript 从网页中提取要发送到 Zemanta 的文本 因此我正在寻找一个可以将我的文本截断为 8 KB 的函数 Zemanta 应该自行执行此截断
  • 是否可以在内存中保留空间以减少加载时间? [关闭]

    Closed 这个问题需要细节或清晰度 目前不接受答案 我想使用 spacy 作为 NLP 来提供在线服务 每次用户发出请求时 我都会调用脚本 my script py 开头为 from spacy en import English nl
  • 如何查看 Fortify 安全编码规则的所有规则?

    我想查看 Fortify 安全编码规则的具体规则 Fortify 默认使用的规则 因为我想编写一份有关 Fortify 使用的所有规则的报告 我试图看到他们C Program Files Fortify Software HP Fortif
  • 为什么我不能将数据集传递给函数?

    我正在使用该包glmulti将模型拟合到多个数据集 如果我一次适合一个数据集 一切都会正常 例如 output lt glmulti y x1 x2 data dat fitfunction lm 工作得很好 但是 如果我创建一个像这样的包
  • Avro 日期和时间与 BigQuery 的兼容性吗?

    BigQuery 通常在加载 Avro 数据方面做得很好 但 bq load 在处理时间戳和使用 Avro 逻辑类型属性的其他日期 时间字段时遇到很多麻烦 当 BigQuery TIMESTAMP 将 Avro 类型 timestamp m
  • 标题窗格 CSS 设置

    我想知道如何将 CSS 设置应用到TitledPane 但找不到任何例子 我想应用自定义设置TitledPane工具栏和背景 但这不起作用 titled pane fx background color linear gradient al
  • Javascript Date(dateString) 在特定服务器和浏览器上返回 NaN

    我正在使用日期格式为 yyyy mm dd 的 Javascript Date string 构造函数 构造函数在 IE 9 和 Firefox 中工作得很好 除非应用程序在运行 IIS 的测试虚拟机上运行 如果它在虚拟机上 在 IE 9
  • Eclipse 模板如何处理驼峰式大小写?

    我正在尝试编写一个 Eclipse 模板 它将在创建属性时创建 getter 和 setter 我当前的模板可以工作 但 getter 和 setter 的驼峰外壳除外 私有 类型 字段 私有 type get field return f
  • 是否可以使用 AWS Beanstalk 的 .ebextensions 配置来安装 mod_pagespeed Apache 模块?

    我正在为我的 Django Python 应用程序使用 AWS Beanstalk 并且我想使用 Google 的 mod pagespeed 模块 是否可以使用 ebextensions config 文件安装并运行 mod pagesp
  • 如何监控20多个区域?

    我正在开发一个有 66 个注释的应用程序 这些注释是区域的中心 每当用户进入某个区域时 就会出现一条通知 但这仅适用于其中的前 20 个 因为监视区域的数量有限 我的问题是我不知道如何监控20多个区域 有人可以帮忙吗 set current
  • 这个紫色虚线区域的用途是什么?

    这个紫色区域代表什么 它显示了可以扩展元素的可用空间 例子 这是一个只有一个字符的示例 可以看到文本后面的虚线区域 这意味着这是文本可以扩展的区域 这是一个包含更多字符的示例 可以看出 长度为紫色虚线面积减少 可以运行以下代码片段并打开开发
  • 从不同的包导入 proto 文件会导致“缺少方法 protoreflect”

    我对 Go 和 Protobufs 很陌生 因此这可能是一个非常菜鸟的问题 对此感到抱歉 我的 go 项目中有几个包 我想创建一个单独的包 其中包含我所有的 proto 也是 pb go 文件 然后我可以将这些 proto 文件导入到任何其