How to Work with Databend in Golang
Prerequisites
- Databend : Make sure Databend is running and accessible, please see How to deploy Databend.
Golang
This guideline show how to connect and query to Databend using Golang.
We will be creating a table named books
and insert a row, then query it.
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
)
const (
username = "root"
password = ""
hostname = "127.0.0.1:3307"
dbname = "book_db"
)
type Book struct {
Title string
Author string
Date string
}
func dsn(dbName string) string {
return fmt.Sprintf("%s:%s@tcp(%s)/%s", username, password, hostname, dbName)
}
func main() {
db, err := sql.Open("mysql", dsn(dbname))
if err != nil {
log.Fatal(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
log.Fatal(err)
}
log.Println("Connected")
// Create table.
sql := "create table if not exists books(title varchar(255), author varchar(255), date varchar(255))"
_, err = db.Exec(sql)
if err != nil {
log.Fatal(err)
}
log.Println("Create table: books")
// Insert 1 row.
sql = "insert into books values('mybook', 'author', '2022')"
_, err = db.Exec(sql)
if err != nil {
log.Fatal(err)
}
log.Println("Insert 1 row")
// Select.
res, err := db.Query("SELECT * FROM books")
if err != nil {
log.Fatal(err)
}
for res.Next() {
var book Book
err := res.Scan(&book.Title, &book.Author, &book.Date)
if err != nil {
log.Fatal(err)
}
log.Printf("Select:%v", book)
}
}