导入 `github.com/influxdb/influxdb/client/v2` 包时缺少符号

Missing symbols when importing `github.com/influxdb/influxdb/client/v2` package

在 Golang 的 google 云上设置网络套接字,并导入在我的本地机器上运行良好的代码在云上不起作用。

我有:

import "github.com/influxdb/influxdb/client/v2"

并且有 运行

go get "github.com/influxdb/influxdb/client/v2"

运行宁去 运行 server.go 我得到:

# command-line-arguments
./pi_server.go:47: undefined: client.NewClient
./pi_server.go:47: undefined: client.Config

下面的完整代码,不包括 const 声明和 html:

package main

import (
    "flag"
    "html/template"
    "log"
    "net/http"
    "github.com/gorilla/websocket"
    "fmt"
    "net/url"
    "github.com/influxdb/influxdb/client/v2"
    "time"
)

var addr = flag.String("addr", "localhost:8080", "http service address")

var upgrader = websocket.Upgrader{} // use default options

func echo(w http.ResponseWriter, r *http.Request) {

    //Influx init
    u,err := url.Parse("http://localhost:8086")
    checkError(err)
    influx_c := client.NewClient(client.Config{
            URL: u,
            Username: username,
            Password: password,
     })
    bp,err := client.NewBatchPoints(client.BatchPointsConfig{
        Database:  MyDB,
        Precision: "s",
    })
    tags := map[string]string{"my_sensor_id": my_sensor_id}
    //end influx init



    c, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Print("upgrade:", err)
        return
    }
    defer c.Close()
    for {
        mt, message, err := c.ReadMessage()
        if err != nil {
            log.Println("read:", err)
            break
        }
        log.Printf("recv: %s", message)

        /*
            write to influx here
        */ 
        fields := map[string]interface{}{
            "random_int":   message,
            "other_stuff":  69696,
        }
        pt,err := client.NewPoint("test_collection", tags, fields, time.Now())
        checkError(err)
        bp.AddPoint(pt)
        influx_c.Write(bp)



        err = c.WriteMessage(mt, message)
        if err != nil {
            log.Println("write:", err)
            break
        }
    }
}

func home(w http.ResponseWriter, r *http.Request) {
    homeTemplate.Execute(w, "ws://"+r.Host+"/echo", )
}

func main() {


    flag.Parse()
    log.SetFlags(0)
    http.HandleFunc("/echo", echo)
    http.HandleFunc("/", home)
    log.Fatal(http.ListenAndServe(*addr, nil))
}

您的本地计算机在 this commit 之前的版本为 github。com/influxdb/influxdb/client/v2。您的云服务器正在获取更新版本的包。

要解决此问题,运行

go get -u github.com/influxdb/influxdb/client/v2

在您的本地计算机上获取最新版本的软件包。更新应用程序代码以使用新函数和类型名称:

influx_c := client.NewHTTPClient(client.HTTPConfig{
        URL: u,
        Username: username,
        Password: password,
 })

搞定了,谢谢!另请注意以下代码:

influx_c,err := client.NewHTTPClient(client.HTTPConfig{
    Addr: "http://localhost:8086",
    Username: username,
    Password: password,
})

他们将 URL 字段更改为 Addr,使用的是字符串文字而不是 net/url 对象