Golang Basic Example
Configure the environment
Before you start, make sure you have downloaded and installed the following software:
-
Confirm that you have completed the installation of the MySQL client.
-
Completed Create instance and connect to MatrixOne Intelligence through the MySQL client and create a database named test:
mysql> create database test; -
Confirm that you have completed the installation [Golang version 1.18 and above] (https://go.dev/dl/), and you can use the following command line to confirm your Golang version:
#To check with Golang installation and its version go version -
Confirm that you have installed
gorm.io/driver/mysql, and use thego getcommand to install it. The code is as follows:go get -u gorm.io/driver/mysql
You can refer to Golang Connect MatrixOne Intelligence Service to learn how to connect to MatrixOne Intelligence through Golang. This document will guide you how to implement CRUD (create, read, update, delete).
Steps
-
Connect to MatrixOne Intelligence through the MySQL client. Create a new database called test.
mysql> create database test; -
Create a plain text file named
golang_crud_matrixonecloud.goand copy the following code into the file:package main import ( "database/sql" "fmt" "log" _ "github.com/go-sql-driver/mysql" ) func main() { //Open a new connection to MatrixOne Intelligence 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) dbConnectionString := encodedUsername + ":" + password + "@tcp(" + host + ":" + strconv.Itoa(port) + ")/" + database db, err := sql.Open("mysql", dbConnectionString) checkErr(err) //Create a table _, err2 := db.Exec("CREATE TABLE `userinfo` (`uid` INT(10) NOT NULL AUTO_INCREMENT,`username` VARCHAR(64) NULL DEFAULT NULL,`department` VARCHAR(64) NULL DEFAULT NULL,`created` DATETIME NULL DEFAULT NULL, PRIMARY KEY (`uid`));") if err2 != nil { log.Fatal(err2) } fmt.Print("Successfully Created\n") // Insert a record stmt, err := db.Prepare("INSERT userinfo SET username=?,department=?,created=?") checkErr(err) res, err := stmt.Exec("Alex", "r&d", "2023-01-01 12:00:00") checkErr(err) id, err := res.LastInsertId() checkErr(err) fmt.Println(id) //Update a record stmt, err = db.Prepare("update userinfo set username=? where uid=?") checkErr(err) res, err = stmt.Exec("Mark", id) checkErr(err) affect, err := res.RowsAffected() checkErr(err) fmt.Println(affect) // Query all records rows, err := db.Query("SELECT * FROM userinfo") checkErr(err) for rows.Next() { var uid int var username string var department string var created string err = rows.Scan(&uid, &username, &department, &created) checkErr(err) fmt.Println(uid) fmt.Println(username) fmt.Println(department) fmt.Println(created) } // Delete a record stmt, err = db.Prepare("delete from userinfo where uid=?") checkErr(err) res, err = stmt.Exec(id) checkErr(err) affect, err = res.RowsAffected() checkErr(err) fmt.Println(affect) db.Close() } func checkErr(err error) { if err != nil { panic(err) } } -
Open a new terminal, use the following command line to execute this Golang file.
> go run golang_crud_matrixonecloud.go Successfully Created 1 1 1 Mark r&d 2023-01-01 1