」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何解決Go中從不同套件匯入Proto檔案時出現「Missing Method Protoreflect」錯誤?

如何解決Go中從不同套件匯入Proto檔案時出現「Missing Method Protoreflect」錯誤?

發佈於2024-11-05
瀏覽:122

How to Resolve the \

如何從不同的套件導入Proto 檔案而不遇到「Missing Method Protoreflect」錯誤

在Go 中,protobuf 常用於資料序列化。將 protobuf 組織到不同的套件中時,可能會遇到與缺少 ProtoReflect 方法相關的錯誤。當嘗試將資料解組到單獨套件中定義的自訂 protobuf 結構時,會出現此錯誤。

要解決此問題,請確保執行以下步驟:

  1. 確保一致的Protobuf 物件註冊:

    在每個使用自訂protobuf 結構的套件中,應匯入由protoc 產生的對應*.pb.go 檔案。這會將 protobuf 結構註冊到 ProtoReflect 庫,從而實現無縫解組。

  2. 驗證導入語法:

    檢查包中的導入語句,其中執行解組。確保導入路徑匹配:

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

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

[&google&& ]根據現有相依性使用適當的導入路徑。

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

考慮定義了protobuf 檔案的專案結構在一個名為prototemps.
package reader

import (
    "google.golang.org/protobuf/proto"
)

要解決reader 套件中的錯誤,請確保使用正確的導入語句:

How to Resolve the \套件閱讀器 進口 ( “google.golang.org/protobuf/proto” )

透過執行下列步驟,您可以成功從不同的套件匯入 proto 檔案並將資料解組到自訂 protobuf 結構中,而不會遇到「缺少方法 protoreflect」錯誤。

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3