Connecting to OmniFabric with Golang

OmniFabric supports Golang application connection, Go-MySQL-Driver is supported. This tutorial will walk you through how to connect OmniFabric with Golang.

Before you start

  1. Make sure you have already installed and launched OmniFabric.
  2. Make sure you have already installed Golang 1.18 and plus.

    #To check with Golang installation and its version
    go version
    
  3. Make sure you have already installed MySQL client.

Using Golang to connect to OmniFabric

Go-MySQL-Driver is a MySQL driver for Go's (golang) database/sql package.

  1. Install go-mysql-driver tool. Simple install the package to your $GOPATH with the go tool from shell. Make sure Git is installed on your machine and in your system's PATH.

    > go get -u github.com/go-sql-driver/mysql
    
  2. Connect to OmniFabric by MySQL client. Create a new database named test.

    mysql> create database test;
    
  3. Create a plain text file golang_connect_OmniFabric.go and put the code below.

    package main
    
    import (
    "database/sql"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
    )
    
    func main() {
    //"username:password@[protocol](address:port)/database"
    db, _ := sql.Open("mysql", "root:111@tcp(127.0.0.1:6001)/test") // Set database connection
    defer db.Close()                                            //Close DB
    err := db.Ping()                                            //Connect to DB
    if err != nil {
        fmt.Println("Database Connection Failed")               //Connection failed
        return
    } else {
        fmt.Println("Database Connection Succeed")              //Connection succeed
    }
    }
    
  4. Execute this golang file in the command line terminal.

    > go run golang_connect_OmniFabric.go
    Database Connection Succeed
    

Using Gorm to connect to OmniFabric

Gorm is the fantastic ORM library for Golang and aims to be developer-friendly. gorm.io/gorm and gorm.io/driver/mysql will make Go connect to MYSQL.

  1. Use command go get to install gorm.io/gorm and gorm.io/driver/mysql.

    go get -u gorm.io/gorm
    go get -u gorm.io/driver/mysql
    
  2. Connect to OmniFabric by MySQL client. Create a new database named test.

    mysql> create database test;
    
  3. Create a go file golang_gorm_connect_OmniFabric.go and put the code below.

    go package main import ( "gorm.io/driver/mysql" "gorm.io/gorm" "fmt" ) func getDBConn() *gorm.DB { dsn := "root:111@tcp(127.0.0.1:6001)/test?charset=utf8mb4&parseTime=True&loc=Local" //MO db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ }) // get connection if err != nil { fmt.Println("Database Connection Failed") //Connection failed } else { fmt.Println("Database Connection Succeed") //Connection succeed } return db } func main() { getDBConn() }

  4. Execute this go file in the command line terminal.

    > go run golang_gorm_connect_OmniFabric.go
    Database Connection Succeed
    

Reference

For the example about using Golang to build a simple CRUD with OmniFabric, see Build a simple Golang CRUD demo with OmniFabric.

For the example about using Gorm to build a simple CRUD with OmniFabric, see the Build a simple Gorm CRUD demo with OmniFabric tutorial.