如何对这个指针的结构进行解引用?

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

How can I dereference the struct of this pointer

问题

我已经翻译了你提供的代码部分,以下是翻译的结果:

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/kr/pretty"
	"googlemaps.github.io/maps"
)

func main() {
	api_key := "xxxxxxx"
	c, err := maps.NewClient(maps.WithAPIKey(api_key))
	if err != nil {
		log.Fatalf("致命错误:%s", err)
	}

	r := &maps.DistanceMatrixRequest{
		Origins:      []string{"达曼"},
		Destinations: []string{"利雅得"},
		ArrivalTime:  (time.Date(1, time.September, 1, 0, 0, 0, 0, time.UTC)).String(),
	}

	resp, err := c.DistanceMatrix(context.Background(), r)
	if err != nil {
		log.Fatalf("致命错误:%s", err)
	}

	pretty.Println(resp)
}

获取的输出为:

&maps.DistanceMatrixResponse{
    OriginAddresses:      {"沙特阿拉伯达曼"},
    DestinationAddresses: {"沙特阿拉伯利雅得"},
    Rows:                 {
        {
            Elements: {
                &maps.DistanceMatrixElement{
                    Status:            "OK",
                    Duration:          14156000000000,
                    DurationInTraffic: 0,
                    Distance:          maps.Distance{HumanReadable:"410 公里", Meters:409773},
                },
            },
        },
    },
}

根据上面的输出,我需要打印出 DurationDistance.HumanReadable,即我需要获取:14156000000000410 公里

因此,我可以将行程持续时间转换为小时和分钟,如下所示:

   value := 14156000000000 // value 是 int 类型
    d2 := time.Duration(value)

	hr := int64(d2 / time.Hour)
	min := int64(d2 / time.Minute)
	m := min - hr*60
	fmt.Printf("所需时间为:%d 小时 %d 分钟", hr, m)

我尝试了以下代码:

	x := resp.Rows[0].Elements
	fmt.Println("持续时间:", x)

但输出结果为:

持续时间: [0xc000192840]
英文:

I've the below code using Google Map Matrix:

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/kr/pretty"
	"googlemaps.github.io/maps"
)

func main() {
	api_key := "xxxxxxx"
	c, err := maps.NewClient(maps.WithAPIKey(api_key))
	if err != nil {
		log.Fatalf("fatal error: %s", err)
	}

	r := &maps.DistanceMatrixRequest{
		Origins:      []string{"Dammam"},
		Destinations: []string{"Riyadh"},
		ArrivalTime:  (time.Date(1, time.September, 1, 0, 0, 0, 0, time.UTC)).String(),
	}

	resp, err := c.DistanceMatrix(context.Background(), r)
	if err != nil {
		log.Fatalf("fatal error: %s", err)
	}

	pretty.Println(resp)
}

And getting the output as:

&maps.DistanceMatrixResponse{
    OriginAddresses:      {"Dammam Saudi Arabia"},
    DestinationAddresses: {"Riyadh Saudi Arabia"},
    Rows:                 {
        {
            Elements: {
                &maps.DistanceMatrixElement{
                    Status:            "OK",
                    Duration:          14156000000000,
                    DurationInTraffic: 0,
                    Distance:          maps.Distance{HumanReadable:"410 km", Meters:409773},
                },
            },
        },
    },
}

From the output above, I need to print the Duration and the Distance.HumanReadable, i.e.
I need to get: 14156000000000 and 410 km

So I can convert the trip duration to hrs and minutes as:

   value := 14156000000000 // value is of type int
    d2 := time.Duration(value)

	hr := int64(d2 / time.Hour)
	min := int64(d2 / time.Minute)
	m := min - hr*60
	fmt.Printf("Time required is: %d H and %d M", hr, m)

I tried:

	x := resp.Rows[0].Elements
	fmt.Println("Duration:", x)

But the output was:

Duration: [0xc000192840]

答案1

得分: 2

resp.Rows[0].Elements 是一个切片([]*DistanceMatrixElement),所以你需要指定你想要输出的元素。我猜你想要的是:

x := resp.Rows[0].Elements[0].Duration
fmt.Println("Duration:", x)

playground

英文:

resp.Rows[0].Elements is a slice ([]*DistanceMatrixElement) so you will need to specify which element you wish to output. I'd guess that you want:

x := resp.Rows[0].Elements[0].Duration
fmt.Println("Duration:", x)

playground

huangapple
  • 本文由 发表于 2022年8月18日 05:46:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/73395254.html
匿名

发表评论

匿名网友

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

确定