neo-client is the Go client module for Machbase Neo.
It provides:
machgo: the main client package used by applicationsmachbase: the standarddatabase/sqldriver packageapi: shared interfaces, options, and helper typesmachnet: lower-level protocol and transport implementation used bymachgo
The examples in this module show how to connect to a Machbase Neo server, execute queries, append time-series records, and use the standard database/sql API.
- Go 1.22 or later
- A reachable Machbase Neo server
- A valid user account, such as
sys/managerin a local development environment
The examples in this repository use the native TCP endpoint, usually 127.0.0.1:5656.
go get github.com/machbase/neo-clientUse this package for normal application code. It exposes the database handle, connection management, query execution, prepared statements, and append operations.
This package contains interfaces such as Database, Conn, Rows, and Appender, plus options like api.WithPassword, api.WithFetchRows, and api.WithStatementCache.
This package provides a standard Go database/sql driver on top of the native TCP client. Use it when you want to integrate Machbase Neo with libraries or application code that already expect the standard database/sql interfaces.
This is the lower-level client implementation used internally by machgo. Most application code should not need to import it directly.
The following example connects to a server and reads from the M$SYS_TABLES system table.
package main
import (
"context"
"fmt"
"github.com/machbase/neo-client/api"
"github.com/machbase/neo-client/machgo"
)
func main() {
db, err := machgo.NewDatabase(&machgo.Config{
Host: "127.0.0.1",
Port: 5656,
MaxOpenConn: -1,
})
if err != nil {
panic(err)
}
defer db.Close()
ctx := context.Background()
conn, err := db.Connect(ctx, api.WithPassword("sys", "manager"))
if err != nil {
panic(err)
}
defer conn.Close()
rows, err := conn.Query(ctx, `SELECT NAME, ID, TYPE FROM M$SYS_TABLES ORDER BY NAME`)
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
var (
name string
id int64
typ int
)
if err := rows.Scan(&name, &id, &typ); err != nil {
panic(err)
}
fmt.Println(name, id, typ)
}
if err := rows.Err(); err != nil {
panic(err)
}
}The append example assumes a tag table named EXAMPLE exists with NAME, TIME, and VALUE columns.
CREATE TAG TABLE IF NOT EXISTS example (
name VARCHAR(100) PRIMARY KEY,
time DATETIME BASETIME,
value DOUBLE
);Then append rows with conn.Appender:
package main
import (
"context"
"time"
"github.com/machbase/neo-client/api"
"github.com/machbase/neo-client/machgo"
)
func main() {
db, err := machgo.NewDatabase(&machgo.Config{
Host: "127.0.0.1",
Port: 5656,
MaxOpenConn: -1,
})
if err != nil {
panic(err)
}
defer db.Close()
ctx := context.Background()
conn, err := db.Connect(ctx, api.WithPassword("sys", "manager"))
if err != nil {
panic(err)
}
defer conn.Close()
appender, err := conn.Appender(ctx, "EXAMPLE")
if err != nil {
panic(err)
}
ts := time.Now()
for i := 0; i < 10; i++ {
if err := appender.Append(
"example-client",
ts.Add(time.Duration(i)*time.Second),
3.14*float64(i),
); err != nil {
panic(err)
}
}
success, fail, err := appender.Close()
if err != nil {
panic(err)
}
println("Append finished.", "success:", success, "fail:", fail)
}If your application already uses database/sql, import github.com/machbase/neo-client for driver registration and connect with sql.Open.
package main
import (
"context"
"database/sql"
"fmt"
_ "github.com/machbase/neo-client"
)
func main() {
dsn := "server=tcp://sys:manager@127.0.0.1:5656;fetch_rows=777;statement_cache=off;io_metrics=true"
db, err := sql.Open("machbase", dsn)
if err != nil {
panic(err)
}
defer db.Close()
ctx := context.Background()
rows, err := db.QueryContext(ctx, `SELECT * FROM M$SYS_TABLES ORDER BY NAME`)
if err != nil {
panic(err)
}
defer rows.Close()
cols, err := rows.Columns()
if err != nil {
panic(err)
}
fmt.Println("Columns:", cols)
for rows.Next() {
// scan values here
}
if err := rows.Err(); err != nil {
panic(err)
}
}Supported DSN keys for the standard driver include:
server: server address such astcp://sys:manager@127.0.0.1:5656host,port,user,password: explicit connection fieldsfetch_rows: fetch batch sizestatement_cache:auto,on, oroffio_metrics:trueorfalsealternative_servers: alternative server list such as127.0.0.2:5656
The standard driver follows database/sql pooling through sql.DB. Explicit transaction statements are not supported by Machbase Neo, so Begin and BeginTx return an error. LastInsertId is also not supported.
Four runnable examples are included under _example/.
Run the query example:
go run ./_example/query.go -s 127.0.0.1:5656 -u sys -p managerRun the append example:
go run ./_example/append.go -s 127.0.0.1:5656 -u sys -p managerRun the standard driver query example:
go run ./_example/driver_query.go -s 127.0.0.1:5656 -u sys -p managerRun the standard driver insert example:
go run ./_example/driver_insert.go -s 127.0.0.1:5656 -u sys -p managerapi.WithPassword(user, password): connect with explicit credentialsapi.WithFetchRows(n): override the default fetch batch size for a connectionapi.WithStatementCache(mode): control query statement reuseapi.WithIOMetrics(true): enable I/O metrics collection on the connection
- Always close
Rows,Stmt,Appender, andConnobjects after use. Appender.Close()returns success and failure counts for the append session.- For regular application usage, prefer
machgoover importingmachnetdirectly. - Use
machbasewhen you need compatibility withdatabase/sqland its connection pooling. - The standard driver does not support explicit transactions or
LastInsertId.