[英]How can I dereference the struct of this pointer
I've the below code using Google Map Matrix:我使用谷歌 Map 矩阵有以下代码:
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:并获得 output 为:
&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
, ie I need to get: 14156000000000
and 410 km
从上面的 output 中,我需要打印
Duration
和Distance.HumanReadable
,即我需要得到: 14156000000000
和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:但是 output 是:
Duration: [0xc000192840]
resp.Rows[0].Elements
is a slice ( []*DistanceMatrixElement
) so you will need to specify which element you wish to output. resp.Rows[0].Elements
是一个切片( []*DistanceMatrixElement
),因此您需要指定要 output 的元素。 I'd guess that you want:我猜你想要:
x := resp.Rows[0].Elements[0].Duration
fmt.Println("Duration:", x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.