gpt4 book ai didi

go - 如何使用第三方包中未导出的对象作为 golang 中的返回类型?

转载 作者:IT王子 更新时间:2023-10-29 01:54:52 26 4
gpt4 key购买 nike

在我的用例中,我使用的是 influxdb。我正在尝试在 Golang 中为 influxdb 创建一个全局客户端。但是当我从函数返回 influxdb 客户端时,客户端对象不会在 influxdb 包中导出。所以我无法返回。

这是我的代码:

package influxclient

import (
"github.com/influxdata/influxdb/client/v2"
"log"
"time"
//"net/http"
"fmt"
"reflect"
)

const (
INFLUXDB_NAME = "XXXX"
USERNAME = "YYYY"
PASSWORD = "ZZZZ"
HOST = "http://localhost:8086"
)

var (
c = getHTTPClient()
)

func test() {
// Create a new point batch
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
Database: INFLUXDB_NAME,
Precision: "s",
})

// Create a point and add to batch
tags := map[string]string{"cpu": "cpu-total"}
fields := map[string]interface{}{
"idle": 10.1,
"system": 53.3,
"user": 46.6,
}
fmt.Println(reflect.TypeOf(c))
pt, _ := client.NewPoint("cpu_usage", tags, fields, time.Now())
bp.AddPoint(pt)

// Write the batch
c.Write(bp)
}

//publish metrics to metrics db
func PublishMetrics(metricName string, tags map[string]string, fields map[string]interface{}, time time.Time) error {
fmt.Println("type of client c ", reflect.TypeOf(c))
// Create a new point batch
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: INFLUXDB_NAME,
Precision: "s",
})
if err != nil {
return err
}
pt, err := client.NewPoint(metricName, tags, fields, time)
if err != nil {
return err
}
bp.AddPoint(pt)
// Write the batch
c.Write(bp)
return nil
}


func getHTTPClient() *client.client {
//make http client for metrics db
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: HOST,
Username: USERNAME,
Password: PASSWORD,
})
if err != nil {
log.Printf("FATAL :: Error occured in getting influxdb metric client %s ", err.Error())
}
return c
}

getHTTPClient() 函数试图返回客户端对象 *client.client
但是在influxdb golang包中并没有导出client对象。
那么我该如何处理这个问题呢?
谁能帮我解决这个问题?

谢谢

最佳答案

包客户端//import "github.com/influxdata/influxdb/client/v2"
已导出 Client 接口(interface),因此请使用 client.Client 而不是 *client.client:

package influxclient

import (
"log"
"time"

"github.com/influxdata/influxdb/client/v2"
//"net/http"
"fmt"
"reflect"
)

const (
INFLUXDB_NAME = "XXXX"
USERNAME = "YYYY"
PASSWORD = "ZZZZ"
HOST = "http://localhost:8086"
)

var c = getHTTPClient()

func test() {
// Create a new point batch
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
Database: INFLUXDB_NAME,
Precision: "s",
})

// Create a point and add to batch
tags := map[string]string{"cpu": "cpu-total"}
fields := map[string]interface{}{
"idle": 10.1,
"system": 53.3,
"user": 46.6,
}
fmt.Println(reflect.TypeOf(c))
pt, _ := client.NewPoint("cpu_usage", tags, fields, time.Now())
bp.AddPoint(pt)

// Write the batch
c.Write(bp)
}

//publish metrics to metrics db
func PublishMetrics(metricName string, tags map[string]string, fields map[string]interface{}, time time.Time) error {
fmt.Println("type of client c ", reflect.TypeOf(c))
// Create a new point batch
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: INFLUXDB_NAME,
Precision: "s",
})
if err != nil {
return err
}
pt, err := client.NewPoint(metricName, tags, fields, time)
if err != nil {
return err
}
bp.AddPoint(pt)
// Write the batch
c.Write(bp)
return nil
}

func getHTTPClient() client.Client {
//make http client for metrics db
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: HOST,
Username: USERNAME,
Password: PASSWORD,
})
if err != nil {
log.Printf("FATAL :: Error occured in getting influxdb metric client %s ", err.Error())
}
return c
}

关于go - 如何使用第三方包中未导出的对象作为 golang 中的返回类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38290302/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com