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

导入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,
     })
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dousou1878 2016-01-26 05:28

    Nailed it, thanks! Also note from following code:

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

    They changed URL field to Addr, with is a string literal instead of a net/url object

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题