dongyumiao5210 2016-01-26 04:54
浏览 304
已采纳

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

Setting up a web socket on google cloud in Golang, and import code that works fine on my local machine does not work on the cloud.

I have:

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

and have run

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

Upon running go run server.go I get:

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

Full code below, excluding const declarations and 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))
}
  • 写回答

2条回答 默认 最新

  • dongliuzhuan1219 2016-01-26 05:08
    关注

    You local machine has a version of github.com/influxdb/influxdb/client/v2 before this commit. Your cloud server is fetching a more recent version of the package.

    To fix the issue, run

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

    on your local machine to get the latest version of the package. Update the application code to use the new function and type names:

    influx_c := client.NewHTTPClient(client.HTTPConfig{
            URL: u,
            Username: username,
            Password: password,
     })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)