import ( "github.com/influxdb/influxdb/client/v2" ) // create a new client to connect to the InfluxDB server c, err := client.NewHTTPClient(client.HTTPConfig{ Addr: "http://localhost:8086", }) if err != nil { panic(err) } // define some data points to write to the server bp, err := client.NewBatchPoints(client.BatchPointsConfig{ Database: "mydb", Precision: "s", }) if err != nil { panic(err) } tags := map[string]string{"cpu": "cpu-total"} fields := map[string]interface{}{ "idle": 10.1, "system": 53.3, "user": 46.6, } pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now()) if err != nil { panic(err) } bp.AddPoint(pt) // write the batch points to the server err = c.Write(bp) if err != nil { panic(err) }
import ( "github.com/influxdb/influxdb/client/v2" ) // create a new client to connect to the InfluxDB server c, err := client.NewHTTPClient(client.HTTPConfig{ Addr: "http://localhost:8086", }) if err != nil { panic(err) } // build a query to fetch data from the server q := client.NewQuery("SELECT idle FROM cpu_usage", "mydb", "s") resp, err := c.Query(q) if err != nil { panic(err) } if resp.Error() != nil { panic(resp.Error()) } // process the results of the query for _, row := range resp.Results[0].Series[0].Values { // do something with row[1], which contains the idle value }In conclusion, the `github.com/influxdb/influxdb/client` package library in Go provides a convenient way to interact with an InfluxDB database server. You can use this library to write data to the server, query data from the server, and perform other operations as needed.