content/shared/influxdb-client-libraries-reference/v3/go.md
The InfluxDB 3 influxdb3-go Go client library integrates with Go scripts and applications
to write and query data stored in an {{% product-name %}} database.
go get github.com/InfluxCommunity/influxdb3-go/v2
The influxdb3-go client library module provides the influxdb3 package.
Import the package:
import (
"github.com/InfluxCommunity/influxdb3-go/v2/influxdb3"
)
NewCreate a client to interact with InfluxDB.
New(config ClientConfig)
Initializes and returns a influxdb3.Client instance with the following:
*flight.Client and functions for querying the database.config: A ClientConfig struct with the following configuration properties:
Host (string): the {{% product-name %}} server URLToken (string): a database token stringDatabase (string): the database to use for writing and querying.Organization (string): Optional. The organization name or ID.HTTPClient (*http.Client): Optional. Specifies a custom HTTP client, TLS configuration, or timeout to use.WriteOptions (*WriteOptions): Optional. Options passed to the write client for writing to the database.Headers (http.Header): Optional. Headers to include in all requests.{{% code-placeholders "DATABASE_(NAME|TOKEN)" %}}
package main
import (
"github.com/InfluxCommunity/influxdb3-go/v2/influxdb3"
)
func main() {
client, err := influxdb3.New(influxdb3.ClientConfig{
Host: "https://{{< influxdb/host >}}",
Token: "DATABASE_TOKEN",
Database: "DATABASE_NAME",
})
defer func(client *influxdb3.Client) {
err := client.Close()
if err != nil {
panic(err)
}
}(client)
if(err != nil) {
panic(err)
}
}
{{% /code-placeholders %}}
Replace the following configuration values:
DATABASE_NAME{{% /code-placeholder-key %}}: the name of the InfluxDB database to queryDATABASE_TOKEN{{% /code-placeholder-key %}}:
an InfluxDB database token
with read permission on the specified databaseClient.Query()Query data from InfluxDB 3 using SQL.
client.Query(ctx context.Context, query string)
Sends a Flight query request with SQL to InfluxDB.
Returns the following:
ctx (context.Context): the context to use for the requestquery (string): the SQL query to execute.query := `SELECT *
FROM home
WHERE time >= '2022-01-02T08:00:00Z'
AND time <= '2022-01-02T20:00:00Z'`
iterator, err := client.Query(context.Background(), query)
Client.QueryWithOptions()Query data from InfluxDB 3 with query options such as query type for querying with InfluxQL.
client.QueryWithOptions(ctx context.Context, options *QueryOptions, query string)
Sends a query request with the specified query options to InfluxDB.
Returns the following:
ctx (context.Context): the context to use for the requestoptions: query options (query type, optional database)query (string): the SQL or InfluxQL query to execute.query := `SELECT *
FROM home
WHERE time >= 1641124000s
AND time <= 1641124000s + 8h`
queryOptions := influxdb3.QueryOptions{QueryType: influxdb3.InfluxQL}
iterator, err := client.QueryWithOptions(context.Background(), &queryOptions, query)