亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無效操作 - 不匹配的類型 string 和 nil

無效操作 - 不匹配的類型 string 和 nil

Go
婷婷同學_ 2022-10-17 15:56:16
我正在嘗試創建一個連接到 Google Cloud API 的 Go RESTful API。發生了一個問題,描述為:無效操作:req.ParentId != nil(字符串和 nil 類型不匹配)在創建本地 API 之前,我設法編寫了一個 Go 模塊,該模塊可以連接、驗證和從 Google Cloud 提取數據。我現在正在將該模塊遷移為帶有路徑的 Restful API。該模塊的重點是獲取所有當前正在運行的虛擬機的列表。這是將其更改為restful API(有效)之前的模塊:package mainimport (    "fmt"    "example.com/compute"    "bytes")func main() {    buf := new(bytes.Buffer)    // Get a message and print it.    r.HandleFunc("/virtualmachines", ListAllInstances)    err := compute.ListAllInstances(buf, "unique-string-id")    if err != nil {        panic(err)    }    fmt.Println(buf.String()) // <======= Print buf contents!}// new file:package compute// [START compute_instances_list_all]import (    "context"    "fmt"    "io"    compute "cloud.google.com/go/compute/apiv1"    "google.golang.org/api/iterator"    computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"    "google.golang.org/protobuf/proto")// listAllInstances prints all instances present in a project, grouped by their zone.func ListAllInstances(w io.Writer, projectID string) error {    // projectID := "your_project_id"    ctx := context.Background()    instancesClient, err := compute.NewInstancesRESTClient(ctx)    if err != nil {        return fmt.Errorf("NewInstancesRESTClient: %v", err)    }    defer instancesClient.Close()    // Use the `MaxResults` parameter to limit the number of results that the API returns per response page.    req := &computepb.AggregatedListInstancesRequest{        Project:    projectID,        MaxResults: proto.Uint32(6),    }    it := instancesClient.AggregatedList(ctx, req)    fmt.Fprintf(w, "Instances found:\n")    // Despite using the `MaxResults` parameter, you don't need to handle the pagination    // yourself. The returned iterator object handles pagination    // automatically, returning separated pages as you iterate over the results.ParentId是罪魁禍首(滾動下方查看完整錯誤)。我插入的字符串在上面的代碼中似乎不起作用。Google Cloud API 正在嘗試找出項目的父 ID。如何修復上面的代碼?
查看完整描述

2 回答

?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

我有同樣的問題,通過降級 1 commit 解決:

 go get google.golang.org/genproto@81c1377

https://github.com/googleapis/go-genproto/commits/main


查看完整回答
反對 回復 2022-10-17
?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

是您的代碼中的錯誤還是只是在發布中?在出版物中我看到

req := &computepb.AggregatedListInstancesRequest{
        Project:    "unqie-string-id,
        MaxResults: proto.Uint32(16),
    }

但應該是

req := &computepb.AggregatedListInstancesRequest{
        Project:    "unqie-string-id",
        MaxResults: proto.Uint32(16),
    }


查看完整回答
反對 回復 2022-10-17
  • 2 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號