导入`github.com/influxdb/influxdb/client/v2`包时出现缺少符号的问题。

huangapple go评论76阅读模式
英文:

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

问题

在Google Cloud上使用Golang设置Web Socket,并导入在本地机器上正常工作的代码,在云端无法正常工作。

我已经执行了以下操作:

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

并运行了

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

运行go run server.go后,我得到了以下错误:

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

以下是完整的代码,不包括常量声明和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))
}

请注意,你需要根据你的实际情况进行适当的配置和更改。

英文:

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

答案1

得分: 2

你的本地机器上有一个早于此提交的 github.com/influxdb/influxdb/client/v2 版本(https://github.com/influxdata/influxdb/commit/b0e9f7e844225b05abf9f4455229490f99348ac4)。你的云服务器正在获取一个更新的版本。

要解决这个问题,在本地机器上运行以下命令:

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

以获取包的最新版本。更新应用程序代码以使用新的函数和类型名称:

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

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

答案2

得分: 0

你好!以下是翻译好的内容:

成功了,谢谢!还请注意以下代码:

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

他们将URL字段更改为Addr,它是一个字符串字面量,而不是net/url对象。

英文:

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

huangapple
  • 本文由 发表于 2016年1月26日 12:54:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/35007249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定