Connecting to MatrixOne with Golang

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

Before you start

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

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

Using Golang to connect to MatrixOne

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.

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

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

``` 1. package main

  1. import (
  2. "database/sql"
  3. "fmt"
  4. _ "github.com/go-sql-driver/mysql"
  5. )

  6. func main() {

  7. //"username:password@protocol/database"
  8. db, _ := sql.Open("mysql", "root:111@tcp(127.0.0.1:6001)/test") // Set database connection
  9. defer db.Close() //Close DB
  10. err := db.Ping() //Connect to DB
  11. if err != nil {
  12. fmt.Println("Database Connection Failed") //Connection failed
  13. return
  14. } else {
  15. fmt.Println("Database Connection Succeed") //Connection succeed
  16. }
  17. } ```
  18. Execute this golang file in the command line terminal.

1. > go run golang_connect_matrixone.go 2. Database Connection Succeed

Using Gorm to connect to MatrixOne

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.

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

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

1. package main 2. import ( 3. "gorm.io/driver/mysql" 4. "gorm.io/gorm" 5. "fmt" 6. ) 7. func getDBConn() *gorm.DB { 8. dsn := "root:111@tcp(127.0.0.1:6001)/test?charset=utf8mb4&parseTime=True&loc=Local" //MO 9. db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ 10. }) 11. // get connection 12. if err != nil { 13. fmt.Println("Database Connection Failed") //Connection failed 14. } else { 15. fmt.Println("Database Connection Succeed") //Connection succeed 16. } 17. return db 18. } 19. func main() { 20. getDBConn() 21. } 4. Execute this go file in the command line terminal.

1. > go run golang_gorm_connect_matrixone.go 2. Database Connection Succeed

Reference

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

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