我正在嘗試學習 golang,并決定構建一個簡單的應用程序來停止我的 gcloud 項目中的實例。下面是相關片段。func stopTaggedMachines(svc *compute.Service, project, zone, tag string) ([]string, error) { //result := computeService.Instances.List("my-project", "us-west1-b") var instances []string f := func(page *compute.InstanceList) error { for _, v := range page.Items { if v.Labels["gcp-idler-managed"] == "true" { result := svc.Instances.Stop(project, zone, v.Name) fmt.Printf("[INFO] gcp-machine-idler: Instance in state %v, Stopping %v... Result - %v\n", v.Status, v.Name, result) instances.append(result) } } return nil } call := svc.Instances.List("my-project", "us-west1-b") if err := call.Pages(oauth2.NoContext, f); err != nil { return instances, nil } return instances, nil}func main() { // Use oauth2.NoContext if there isn't a good context to pass in. ctx := context.Background() computeService, err := compute.NewService(ctx) if err != nil { log.Fatal(err) } stopTaggedMachines(computeService, "my-project", "us-west1-b", "gcp-idler-managed") return}當我運行時,go run main.go我得到的輸出表明機器處于運行狀態(所以我知道我已經到達停止線)。然而,機器從未停止。我有點困惑這里可能出了什么問題,或者(更重要的是)如何找到可以幫助我的資源。我的代碼是否存在邏輯缺陷?更有經驗的 Go 開發人員如何找到有關此方法及其用法的更多信息?從我能找到的來看,文檔似乎相當稀疏?;卮穑焊铝舜a片段...stopTaggedMachines像這樣打電話stopTaggedMachines(ctx, computeService, "my-project", "us-west1-b", "gcp-idler-managed")Stop像這樣打電話result, err := svc.Instances.Stop(project, zone, v.Name).Context(ctx).Do()
1 回答

慕蓋茨4494581
TA貢獻1850條經驗 獲得超11個贊
更改這行代碼:
result := svc.Instances.Stop(project, zone, v.Name)
到:
result, err := svc.Instances.Stop(project, zone, v.Name).Context(ctx).Do()
- 1 回答
- 0 關注
- 211 瀏覽
添加回答
舉報
0/150
提交
取消