我有這個模塊,它使用 Google Cloud API 檢索特定專案的所有正在運行的虛擬機實體的串列。我是 Go 的新手,并按照介紹教程來幫助我。我仍在嘗試除錯我的代碼,但沒有運氣。
問題是我能夠與 Google Cloud API 通信并通過身份驗證,但這就是我所能通過的
compute.go 模塊:
compute.go 能夠與 Google Cloud 服務器通信并通過身份驗證(我沒有收到身份驗證錯誤)
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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)
// instancesClient, err := compute.NewInstancesRESTClient(ctx, option.WithCredentialsFile(`C:\path\to\jsonkey.json`))
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.
for {
pair, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
instances := pair.Value.Instances
if len(instances) > 0 {
fmt.Fprintf(w, "%s\n", pair.Key)
for _, instance := range instances {
fmt.Fprintf(w, "- %s %s\n", instance.GetName(), instance.GetMachineType())
}
}
}
return nil
}
// [END compute_instances_list_all]
但是問題是當我運行呼叫 的主函式時ListAllInstances,它回傳一個<nil>. 不允許我知道哪里出了問題。
我運行的呼叫者 api.go 模塊go run .:
package main
import (
"fmt"
"example.com/compute"
"bytes"
)
func main() {
buf := new(bytes.Buffer)
// Get a message and print it.
respone := compute.ListAllInstances(buf, "project-unique-id")
fmt.Println(respone)
}
我還能如何進一步除錯以找出我的代碼有什么問題?
uj5u.com熱心網友回復:
你不是在列印buf。您的函式回傳一個型別為 的物件error,即nil(沒有錯誤!),實際輸出將寫入buf.
要么列印出來:
func main() {
buf := new(bytes.Buffer)
// Get a message and print it.
err := compute.ListAllInstances(buf, "project-unique-id")
if err != nil {
panic(err)
}
fmt.Println(buf.String()) // <======= Print buf contents!
}
或者只是使用os.Stdout:
func main() {
err := compute.ListAllInstances(os.Stdout, "project-unique-id")
if err != nil {
panic(err)
}
}
要回答有關除錯的問題,請嘗試將 VSCode 與Go 擴展一起使用,在那里您可以運行除錯器、設定斷點并逐行執行代碼,觀察變數如何變化。
另請參閱在 VS Code 中除錯 Go 程式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/371091.html
標籤:走
上一篇:奇怪的方法輸出
下一篇:離子電容器iOS不尊重暗模式
