在https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries中没有使用 Google Analytics Data API v1 的 golang 演示代码
当我编写 golang 代码以使用 Analytics Data API v1 时,会出现如下错误:
C:\Users\qiuskill\go\src\google_api_test>go run main.go
&{0xc0001ac700}
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x1114c1d]
goroutine 1 [running]:
google.golang.org/grpc/internal/grpcsync.(*Event).HasFired(...)
C:/Users/qiuskill/go/pkg/mod/google.golang.org/grpc@v1.40.1/internal/grpcsync/event.go:55
google.golang.org/grpc.(*ClientConn).waitForResolvedAddrs(0xc00011bd20, {0x12b3670, 0xc000022050})
C:/Users/qiuskill/go/pkg/mod/google.golang.org/grpc@v1.40.1/clientconn.go:573 +0x3d
google.golang.org/grpc.newClientStream({0x12b3670, 0xc000022050}, 0x15b3a60, 0xc0001ac700, {0x1237a5c, 0xa}, {0x0, 0x0, 0x0})
C:/Users/qiuskill/go/pkg/mod/google.golang.org/grpc@v1.40.1/stream.go:177 +0x133
google.golang.org/grpc.invoke({0x12b3670, 0xc000022050}, {0x1237a5c, 0xe0}, {0x1207460, 0xc0001e6000}, {0x11ffe20, 0xc0001e8000}, 0x0, {0x0, ...})
C:/Users/qiuskill/go/pkg/mod/google.golang.org/grpc@v1.40.1/call.go:66 +0x7d
google.golang.org/grpc.(*ClientConn).Invoke(0x20aa1a80108, {0x12b3670, 0xc000022050}, {0x1237a5c, 0x0}, {0x1207460, 0xc0001e6000}, {0x11ffe20, 0xc0001e8000}, {0x0, ...})
C:/Users/qiuskill/go/pkg/mod/google.golang.org/grpc@v1.40.1/call.go:37 +0x265
google.golang.org/genproto/googleapis/analytics/data/v1beta.(*betaAnalyticsDataClient).RunReport(0xc00005ba50, {0x12b3670, 0xc000022050}, 0x1, {0x0, 0x0, 0x0})
C:/Users/qiuskill/go/pkg/mod/google.golang.org/genproto@v0.0.0-20220112215332-a9c7c0acf9f2/googleapis/analytics/data/v1beta/analytics_data_api.pb.go:2407 +0xce
main.main()
C:/Users/qiuskill/go/src/google_api_test/main.go:76 +0x28d
exit status 2
下面是我的源代码,我用 ******* 替换了我的属性 ID:
package main
import (
"context"
"fmt"
data "google.golang.org/genproto/googleapis/analytics/data/v1beta"
"google.golang.org/grpc"
)
func main() {
cc := &grpc.ClientConn{}
// fmt.Println(cc)
client := data.NewBetaAnalyticsDataClient(cc)
fmt.Println(client)
ctx := context.Background()
runReportRequest := &data.RunReportRequest{
Property: "properties/*******",
DateRanges: []*data.DateRange{
{StartDate: "2022-01-10", EndDate: "2022-01-10"},
},
Dimensions: []*data.Dimension{
{Name: "city"},
},
Metrics: []*data.Metric{
{Name: "activeUsers"},
},
}
// fmt.Println(runReportRequest)
_, err := client.RunReport(ctx, runReportRequest)
if err != nil {
panic(err)
} else {
// cnt := response.RowCount
// println(string(cnt))
}
}
有没有人知道是什么原因导致出现错误或者任何人都可以写一个golang demo来使用Google Analytics Data API v1,等待您的回答,非常感谢!!!