Skip to main content
Version: 0.7.0

How to Work with Databend in Golang

Before You Begin

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 db if do not exist
dbSql := "create database if not exists book_db"
_, err = db.Exec(dbSql)
if err != nil {
log.Fatal(err)
}
log.Println("Create database book_db success")

// Use book_db database
_, err = db.Exec("use book_db")
if err != nil {
log.Fatal(err)
}

// 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)
}

}