Skip to main content

Connect with Go

Initialize Module

go mod init tdengine.com/example

Add Dependency

add driver-go dependency in go.mod .

go.mod
module tdengine.com/example

go 1.17

require github.com/taosdata/driver-go/v3 latest

view source code

Config

Run this command in your terminal to save DSN(data source name) as variable:

export TDENGINE_GO_DSN="<goDSN>"
IMPORTANT

Replace <goDSN> with the real value, the format should be https(<cloud_endpoint>)/?token=<token>. To obtain the value of goDSN, please log in TDengine Cloud and click "Programming" on the left menu, then select "Go".

Connect

Copy code bellow to main.go.

main.go
package main

import (
"database/sql"
"fmt"
"os"

_ "github.com/taosdata/driver-go/v3/taosRestful"
)

func main() {
dsn := os.Getenv("TDENGINE_GO_DSN")
taos, err := sql.Open("taosRestful", dsn)
if err != nil {
fmt.Println(err)
return
}
defer taos.Close()
rows, err := taos.Query("show databases")
if err != nil {
fmt.Println(err)
return
}
rows.Close()
fmt.Println("connect success")
}

view source code

Then download dependencies by execute command:

go mod tidy

Finally, test the connection:

go run main.go

The client connection is then established. For how to write data and query data, please refer to Data In and Tools.

For more details about how to write or query data via REST API, please check REST API.