Go connector
Install, connect, query and handle errors with the Raft DB Go connector (standard library only).
The Go connector speaks the Raft DB binary protocol (specification) and offers the same operations as every other connector. Download the package from the Download page.
No TLS yet. User names, passwords and tokens travel in clear text. Use only on trusted networks.
Requirements#
Go, standard library only (net, bufio, encoding/binary, encoding/json). Package raftdb.
Install#
Download the module from the Download page and unzip it next to your project. The module path is raftdb.com/connector/go; point it at the unzipped folder with a replace directive in your go.mod:
require raftdb.com/connector/go v0.0.0
replace raftdb.com/connector/go => ./raftdb-go-connectorA vendored copy works the same way: put the folder under any path and replace to it.
Connect#
import raftdb "raftdb.com/connector/go"
// User+Password, or Token, or neither (open server); nil options are fine
db, err := raftdb.Connect(ctx, "127.0.0.1:7480", &raftdb.Options{User: "admin", Password: "s3cret", Timeout: 30 * time.Second})
if err != nil { log.Fatal(err) }
defer db.Close()Query#
existed, err := db.Put("u1", map[string]any{"name": "Asha", "age": 31})
row, err := db.Get("u1") // json.RawMessage; nil if absent
n, err := db.Count(`{"age": {"$gte": 30}}`)
err = db.ScanSQL("SELECT name FROM documents ORDER BY name", 0, func(r json.RawMessage) error {
fmt.Println(string(r)); return nil
})One page of rows (from examples/main.go)#
page, err := db.Query(`{"age": {"$gte": 30}}`, 10, 0)
check(err)
fmt.Println("page rows:", len(page.Rows), "next cursor:", page.Next) // Next 0 = doneErrors#
*raftdb.Error{Code, Message} is a refusal by the server; constants such as CodeBadFilter, CodeSchema, CodeAuth, CodeNotFound exist. Other errors (net errors, ErrClosed) are ordinary errors.
_, err := db.Count(`{"age":`)
var e *raftdb.Error
if errors.As(err, &e) && e.Code == raftdb.CodeBadFilter { fmt.Println(e.Message) }Transactions and prepared statements#
The connector has no transaction call and no prepared-statement call: the protocol has none (every operation is one request and one reply, see the specification), and none of the connector's README, tests or examples uses one. Use the MySQL or MariaDB front-end with a standard driver for those: see Standard MySQL and MariaDB drivers.
Notes#
- A
*Clientmay be shared between goroutines: a mutex serialises the requests (one in flight per connection). - Rows are
json.RawMessage; cursors areuint64. - After an I/O failure, a timeout or an AUTH error the client closes itself.
Conformance status#
24/24 conformance scenarios. The scenarios are listed in the specification page. The package's README.txt has the install steps and a short example.