我正在嘗試創(chuàng)建一個連接到 Google Cloud API 的 Go RESTful API。發(fā)生了一個問題,描述為:無效操作:req.ParentId != nil(字符串和 nil 類型不匹配)在創(chuàng)建本地 API 之前,我設(shè)法編寫了一個 Go 模塊,該模塊可以連接、驗(yàn)證和從 Google Cloud 提取數(shù)據(jù)。我現(xiàn)在正在將該模塊遷移為帶有路徑的 Restful API。該模塊的重點(diǎn)是獲取所有當(dāng)前正在運(yùn)行的虛擬機(jī)的列表。這是將其更改為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是罪魁禍?zhǔn)祝L動下方查看完整錯誤)。我插入的字符串在上面的代碼中似乎不起作用。Google Cloud API 正在嘗試找出項(xiàng)目的父 ID。如何修復(fù)上面的代碼?
無效操作 - 不匹配的類型 string 和 nil
婷婷同學(xué)_
2022-10-17 15:56:16