Skip to content

Connect using Golang

MatrixOne Intelligence supports Golang connections and supports Go-MySQL-Driver.

This document will guide you through how to connect to MatrixOne Intelligence using Golang and Gorm.

Prepare before starting

  • MySQL Client has been installed. If you do not have it, you can click MySQL Client to the official website for download and installation.

  • Create instance has been completed.

  • [Golang version 1.18 and above] (https://go.dev/dl/) has been installed. If you do not have it, you can click [Golang version 1.18 and above] (https://go.dev/dl/) to download and install it. If you have installed it, you can use the following command line to check the version:

    #Check Golang version number to confirm whether it is installed
    go version
    
  • The Git tool has been installed. If you do not have it, you can click Git to download and install it.

Connect to MatrixOne Intelligence Services with Golang

Go-MySQL-Driver is a MySQL driver for the Go language. It implements the database/sql interface method in the Go standard library, allowing Go language programs to connect and operate MySQL databases through this driver.

  1. Install the Go-MySQL-Driver tool:

    Install the Go-MySQL-Driver package to your $GOPATH using Go Tool .

    You can also install the Go-MySQL-Driver tool using the following command line:

    > go get -u github.com/go-sql-driver/mysql
    
  2. Connect to MatrixOne Intelligence using the MySQL client. Create a new database with name test:

    mysql> create database test;
    
  3. Create a plain text file golang_connect_matrixonecloud.go and write the code to the file:

    package main
    
    import (
        "database/sql"
        "fmt"
        "net/url"
        "strconv"
    
        _ "github.com/go-sql-driver/mysql"
    )
    
    func main() {
        username := "585b49fc_852b_4bd1_b6d1_d64bc1d8xxxx:admin:accountadmin" // modify this
        host := "freetier-01.cn-hangzhou.cluster.matrixonecloud.cn" // modify this
        password := "your_password" // modify this
        port := 6001
        database := "test"
        encodedUsername := url.QueryEscape(username)
        dsn := encodedUsername + ":" + password + "@tcp(" + host + ":" + strconv.Itoa(port) + ")/" + database
        db, _ := sql.Open("mysql", dsn) // 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 succeeded
        }
    }
    
  4. Open a terminal and execute the following commands in the terminal:

    > go run golang_connect_matrixonecloud.go
    Database Connection Succeed
    

Connect to MatrixOne Intelligence Services with Gorm

gorm is a magical full-featured ORM library based on golang. We will use the gorm.io/gorm and ``gorm.io/driver/mysql``` to connect Go to the MYSQL database.

  1. Install the gorm.io/gorm`` and ``gorm.io/driver/mysql library, and use the `go get command to install:

    go get -u gorm.io/gorm
    go get -u gorm.io/driver/mysql
    
  2. Connect to MatrixOne Intelligence using the MySQL client. Create a new database with name test:

    mysql> create database test;
    
  3. Create a text file golang_gorm_connect_matrixonecloud.go and write the code to the file:

    package main
    
    import (
        "fmt"
        "net/url"
        "strconv"
    
        "gorm.io/driver/mysql"
        "gorm.io/gorm"
    )
    
    func getDBConn() *gorm.DB {
        username := "585b49fc_852b_4bd1_b6d1_d64bc1d8xxxx:admin:accountadmin" // modify this
        host := "freetier-01.cn-hangzhou.cluster.matrixonecloud.cn" // modify this
        password := "your_password" // modify this
        port := 6001
        database := "test"
        encodedUsername := url.QueryEscape(username)
        dsn := encodedUsername + ":" + password + "@tcp(" + host + ":" + strconv.Itoa(port) + ")/" + database
        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 succeeded
        }
        return db
    }
    func main() {
        getDBConn()
    }
    
  4. Open a terminal and execute the following commands in the terminal:

    > go run golang_gorm_connect_matrixonecloud.go
    Database Connection Succeed
    

Reference Document

For an example of using Golang to build a simple CRUD through MatrixOne Intelligence, see Golang Basic Example.

For an example of using Gorm to build a simple CRUD through MatrixOne Intelligence, see Gorm Basic Example.