gpt4 book ai didi

go - 我如何调试我的 Golang API 代码来告诉我哪里出了问题?

转载 作者:行者123 更新时间:2023-12-04 17:07:56 25 4
gpt4 key购买 nike

我有这个模块,它使用 Google Cloud API 检索特定项目的所有正在运行的虚拟机实例的列表。我是 Go 的新手,并按照介绍教程来帮助我。我仍在尝试调试我的代码,但运气不好。

问题是我能够与 Google Cloud API 通信并通过身份验证,但这就是我所能通过的一切

compute.go模块:

compute.go 能够与谷歌云服务器通信并通过身份验证(我没有收到身份验证错误)

// 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)
}

我还能如何进一步调试它以找出我的代码有什么问题?

最佳答案

您没有打印 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 extension 结合使用,您可以在其中运行调试器、设置断点并逐行执行代码,观察变量如何变化。

另见 Debug Go programs in VS Code .

关于go - 我如何调试我的 Golang API 代码来告诉我哪里出了问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70130013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com