2016-01-26 41 views
0

在Golang的Google雲端設置一個web套接字,並導入在我的本地機器上正常工作的代碼不適用於雲。導入`github.com/influxdb/influxdb/client/v2`包時缺少符號

我:

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)) 
} 

回答

2

本地計算機在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, 
}) 
0

釘住它,謝謝!從下面的代碼還要注意:

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

他們改變URL字段地址,用的是文字,而不是淨/ URL對象的字符串