fmt.Println outputs are numbers instead of strings, how do I get the output to strings? Do I need to some conversion or is something wrong with code?

huangapple go评论89阅读模式
英文:

fmt.Println outputs are numbers instead of strings, how do I get the output to strings? Do I need to some conversion or is something wrong with code?

问题

我的目标是使用Go API客户端获取我在Terraform Cloud帐户中的工作区列表。我编写了一些Go代码,每当我在终端中运行"go run main.go"时,输出显示的是数字而不是工作区名称。以下是Go代码...

package main

import (
	"context"
	"fmt"
	"log"

	tfe "github.com/hashicorp/go-tfe"
)

func main() {
	config := &tfe.Config{
		Token: "my-token",
	}

	client, err := tfe.NewClient(config)
	if err != nil {
		log.Fatal(err)
	}

	// 创建上下文
	ctx := context.Background()

	orgs, err := client.Organizations.List(ctx, nil)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(orgs)

	space, err := client.Workspaces.List(ctx, "orgname", &tfe.WorkspaceListOptions{})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(space, "test")
}

输出结果:

&{0xc0002ae090 [0xc0002b0000]}
&{0xc00000baa0 [0xc000400000 0xc0004001e0 0xc0004003c0 0xc0004005a0 0xc000400780 0xc000400960 0xc000400b40 0xc000400d20 0xc000400f00 0xc0004010e0 0xc0004012c0 0xc0004014a0 0xc000401680 0xc000401860 0xc000401a40 0xc000401c20 0xc000401e00 0xc0004d8000 0xc0004d81e0 0xc0000001e0]} test

我尝试将"orgname"更改为一个虚构的名称,命令行的输出结果为:

&{0xc0000b6ff0 [0xc0000da580]}
2022/03/23 18:10:29 resource not found
exit status 1

这告诉我我所使用的"orgname"是有效的,并且实际上正在获取组织内的工作区。只是输出结果是数字而不是名称。

我期望的结果是:

companyname
[develop__us-east-1__global, develop__us-east-1__compute]
英文:

My goal is to get a list of workspaces in my terraform cloud account using the Go API Client. I've written some Go code, and whenever I run "go run main.go" in my terminal, the output shows numbers rather than the workspace names. Here is the go code...

package main

import (
"context"
"fmt"
"log"

    tfe "github.com/hashicorp/go-tfe"

)

func main() {

    config := &tfe.Config{
    	Token: "my-token",
    }
    
    client, err := tfe.NewClient(config)
    if err != nil {
    	log.Fatal(err)
    }
    
    // Create a Context
    ctx := context.Background()
    
    orgs, err := client.Organizations.List(ctx, nil)
    if err != nil {
    	log.Fatal(err)
    
    }
    
    fmt.Println(orgs)
    
    space, err := client.Workspaces.List(ctx, "orgname", &tfe.WorkspaceListOptions{})
    if err != nil {
    	log.Fatal(err)
    }
    
    fmt.Println(space, "test")

}

The output:

&{0xc0002ae090 \[0xc0002b0000\]}
&{0xc00000baa0 \[0xc000400000 0xc0004001e0 0xc0004003c0 0xc0004005a0 0xc000400780 0xc000400960 0xc000400b40 0xc000400d20 0xc000400f00 0xc0004010e0 0xc0004012c0 0xc0004014a0 0xc000401680 0xc000401860 0xc000401a40 0xc000401c20 0xc000401e00 0xc0004d8000 0xc0004d81e0 0xc0000001e0\]} test

I tried changing the "orgname" to a made up name, the output of the cli is:

&{0xc0000b6ff0 \[0xc0000da580\]}
2022/03/23 18:10:29 resource not found
exit status 1

This tells me that the "orgname" I have works and is actually grabbing workspaces within the organization. Just the output are numbers instead of names.

What I expect:

companyname
[develop__us-east-1__global, develop__us-east-1__compute]

答案1

得分: 1

当你进行测试时,使用Printf中的格式指令可能会很有用。例如:

fmt.Printf("orgs=%+v\n", orgs)

%+v会猜测orgs的类型,并尽可能以人类最好理解其内容的方式显示。例如,如果它是一个指针,它会尝试解引用指针并检查其内容。

根据文档中的说明,Organizations.List()的返回值是*OrganizationList。这是一个指向类型的指针,因此它可以为nil。在代码的后面解引用之前,你需要检查它是否为nil

似乎OrganizationList不能保证返回完整的结果;它嵌入了一个*Pagination指针。但是对于我们的目的,让我们忽略它,因为我们可以假设该对象至少会返回一些结果。

OrganizationList还包括一个Items成员,它是[]*Organization。从这个描述来看,它是一个指向Organization对象的指针切片(可以将其视为动态大小的数组)。在Go中,遍历切片的规范方式是使用range

for index := range orgs.Items {
  fmt.Printf("orgs[%d]: %+v\n", index, orgs.Items[index]);
}

但是这会输出很多内容;一个Organization对象有十几个字段。如果我们只想要名称,可以这样做:

for index := range orgs.Items {
  if orgs.Items[index] != nil {
    fmt.Printf("orgs[%d].Name: %q\n", index, orgs[index].Name);
  }
}
英文:

When you're testing, it can be useful to use the formatting directives in Printf. For instance:

fmt.Printf("orgs=%+v\n", orgs)

The %+v will guess at what orgs is and how a human might best understand its contents. For example, if it's a pointer, it will try to dereference the pointer and inspect it.

According to the documentation, the return value from Organizations.List() is *OrganizationList. That's a pointer to a type, so it can be nil. You'll want to check that before you dereference it later in your code.

It seems like OrganizationList is not guaranteed to return a complete result; it embeds a *Pagination pointer. But for our purposes, let's ignore that, as we can assume the object will return at least a few results.

OrganizationList also includes an Items member, which is []*Organization. Reading this, that's a slice (you can think of this as a dynamically-sized array) of pointers to Organization objects. The canonical way to iterate through a slice in Go is range:

for index := range orgs.Items {
  fmt.Printf("orgs[%d]: %+v\n", index, orgs.Items[index]);
}

But this is going to have a lot of stuff; an Organization object has a dozen or so fields. If all we want is the names, we do this:

for index := range orgs.Items {
  if orgs.Items[index] != nil {
    fmt.Printf("orgs[%d].Name: %q\n", index, orgs[index].Name);
  }
}

答案2

得分: 0

这是因为该对象不是一个字符串类型。
你有几个选项:

  1. 创建一个包装类型,实现 stringer 接口的 String() string 方法,并实现你的逻辑。
  2. 创建一个辅助函数,对该特定类型执行此转换。
  3. 使用 spew.Dump / spew.Sdump,也许你可以找到一些可接受的输出。在这里查看:https://github.com/davecgh/go-spew
  4. 你可以尝试将对象转换为 JSON(或其他格式)并打印出来。也许它可以可读性更好。
英文:

This happens because the object is not a stringer.
You have some options:

  1. Create a wrapper type that implement stringer interface String() string and implement your logic
  2. Create a helper function thar perform this conversion on this particular type
  3. Use spew.Dump / spew.Sdump qnd perhaps you can find some acceptable output. Check here: https://github.com/davecgh/go-spew
  4. You can try to convert the object into a json (or other format) and print it. Perhaps it can be readable

huangapple
  • 本文由 发表于 2022年3月24日 08:14:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/71595570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定