Connect with Go
Initialize Module
go mod init tdengine.com/example
Add Dependency
add driver-go
dependency in go.mod
.
module tdengine.com/example
go 1.17
require github.com/taosdata/driver-go/v3 latest
Config
Run this command in your terminal to save DSN(data source name) as variable:
- Bash
- CMD
- Powershell
export TDENGINE_GO_DSN="<goDSN>"
set TDENGINE_GO_DSN=<goDSN>
$env:TDENGINE_GO_DSN='<goDSN>'
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.
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")
}
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 Insert and Query.
For more details about how to write or query data via REST API, please check REST API.