Go protobuff 导入问题

问题描述 投票:0回答:1

希望能够读取 meshtastic protobuf 数据包。我使用 buf.buid 来生成 SDK。然后我将它们导入到我的 mqtt.go 文件中:

package main

import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"

mqttProto "buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go"
mqtt "github.com/eclipse/paho.mqtt.golang"
"google.golang.org/protobuf/proto"
)

但是,当我尝试使用 go run mqtt.go 运行时,我收到此错误:

no required module provides package buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go; to add it: go get buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go

当我尝试使用 go get buf.build/... 时,我得到一个空输出,问题仍然存在。

我还尝试运行 go mod tidy 并收到此错误:

go: finding module for package buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go
go: router imports buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go: module buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go@latest found (v1.32.0-20240228025038-216ffc09a4e9.1), but does not contain package buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go

我也尝试过运行 go clean -modcache 但没有帮助。关于如何解决这个问题有什么想法吗?谢谢

go protocol-buffers protobuf-go
1个回答
0
投票

您可以通过

buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go
来查看
go get
处的 go 模块实际包含的内容,然后检查其在模块缓存中的内容。模块缓存位于
$GOPATH/pkg/mod
。从那里您应该能够通过导入路径的文件夹导航到模块。

在这种情况下,模块本身没有 Go 文件,只有

go.mod
LICENSE
。所有 Go 文件都在子文件夹中
meshtastic

意思是导入路径应该是:

import mqttProto "buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go/meshtastic"

进行更改应该可以解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.