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 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多