Home > Blockchain >  fmt.Println outputs are numbers instead of strings, how do I get the output to strings? Do I need to
fmt.Println outputs are numbers instead of strings, how do I get the output to strings? Do I need to

Time:03-24

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]

CodePudding user response:

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

CodePudding user response:

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
  • Related