[Alpha] Go SDK
Contents
[Alpha] Go SDK#
The current functionality support of the Go SDK is not yet complete. It is currently only recommended for development, testing, or specific use cases. It is not recommended for use in a production environment. For production use, we recommend using the Java SDK, which has the most comprehensive feature coverage and has undergone extensive testing for both functionality and performance.
Requirement#
OpenMLDB version: >= v0.6.2
Deploy and run APIServer (refer to APIServer deployment document)
Go SDK installation#
go get github.com/4paradigm/openmldb-go-sdk
Go SDK usage#
This section describes the basic use of Go SDK.
Connect to OpenMLDB#
The Go SDK needs to be connected to the API server.
db, err := sql.Open("openmldb", "openmldb://127.0.0.1:8080/test_db")
The format of data source (DSN) is:
openmldb://API_SERVER_HOST[:API_SERVER_PORT]/DB_NAME
You must connect to an existing database.
Create Table#
Create a table demo
:
db.ExecContext(ctx, "CREATE TABLE demo(c1 int, c2 string);")
Insert data#
Insert date into table:
db.ExecContext(ctx, `INSERT INTO demo VALUES (1, "bb"), (2, "bb");`)
Query#
rows, err := db.QueryContext(ctx, `SELECT c1, c2 FROM demo;`)
if err != nil{
panic(err)
}
var col1 int
var col2 string
for rows.Next() {
if err := rows.Scan(&col1, &col2); err != nil {
panic(err)
}
// process row ...
}
Example#
package main
import (
"context"
"database/sql"
// Load OpenMLDB SDK
_ "github.com/4paradigm/openmldb-go-sdk
)
func main() {
db, err := sql.Open("openmldb", "openmldb://127.0.0.1:8080/test_db")
if err != nil {
panic(err)
}
defer db.Close()
ctx := context.Background()
if _, err := db.ExecContext(ctx, `CREATE TABLE demo (c1 int, c2 string);`); err != nil {
panic(err)
}
if _, err := db.ExecContext(ctx, `INSERT INTO demo VALUES (1, "bb"), (2, "bb");`); err != nil {
panic(err)
}
rows, err := db.QueryContext(ctx, `SELECT c1, c2 FROM demo;`)
if err != nil{
panic(err)
}
var col1 int
var col2 string
for rows.Next() {
if err := rows.Scan(&col1, &col2); err != nil {
panic(err)
}
println(col1, col2)
}
}