douqingzhi0980 2016-07-10 09:06
浏览 65
已采纳

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

In my use case I am using influxdb. I am trying to create a global client to influxdb in Golang. But when I return the influxdb client from a function, the client object is not exported in the influxdb package. So I am not able to return this.

Here is my code:

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() function is trying to return the client object *client.client.
But the client object is not exported in influxdb golang package.
So how can I handle this problem?
Could anyone help me with this?

Thanks

  • 写回答

1条回答 默认 最新

  • dongqiao8417 2016-07-10 09:58
    关注

    the package client // import "github.com/influxdata/influxdb/client/v2"
    has exported Client interface so use client.Client instead of *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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗