Important
New database features are no longer connected to engine versions; features are now enabled independently during scheduled update windows, and “major” and “minor” releases no longer exist in Helios. Please visit the release notes to view the latest features available in your database clusters.
Connect with Go
On this page
Use the Go SingleStore driver (go-singlestore-driver), a Go driver for the database/sql driver package, to connect Go applications to SingleStore databases.
Refer to the Go-SingleStore-Driver GitHub repository for more information on supported features and enhancements.
Prerequisites
Install the Go SingleStore Driver
Install the go-singlestore-driver package using the Go CLI utility:
go get -u github.com/singlestore-labs/go-singlestore-driver/v2
Configure the Connection to SingleStore
Use the following connection configuration parameters in the standard MySQL DSN (Data Source Name) or other applicable methods:
-
protocol: Network transport type used to establish the database connection, which determines how the accompanying address is interpreted.Use the tcpprotocol for connecting with SingleStore Helios. -
hostname: IP address or hostname of the SingleStore cluster.Specified when the protocol is tcp.Refer to SingleStore Helios Endpoints to determine the endpoint (
host:port) of your deployment. -
port: Port of the SingleStore cluster.The default is 3306.Specified when the protocol is tcp. -
username: Username of the SingleStore database user. -
password: Password for the SingleStore database user. -
database: Name of the SingleStore database to connect with. -
param=value: (Optional) Additional connection parameters.
The driver supports the following DSN format:
<username>:<password>@tcp(<hostname:port>)/<dbname>[?param=value]
For more information on the DSN and supported parameters, refer to DSN (Data Source Name).
Example
The following example connects to a SingleStore database and performs basic CRUD operations.
-
Create a project folder and initialize the Go module.
mkdir ssGocd ssGogo mod init ssGo -
Install the
go-singlestore-driverpackage.go get -u github.com/singlestore-labs/go-singlestore-driver/v2 -
Create a main.
go file and add the following code to this file. Update the connection configuration of your SingleStore database in the code. package mainimport ("database/sql""fmt""log"_ "github.com/singlestore-labs/go-singlestore-driver/v2")func main() {// Connection settingshost := "svc-xxxx"port := "3306"user := "s2user"password := "p455w1kd"database := "demo"// SingleStore DSNdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true",user,password,host,port,database,)// Open connection using the SingleStore driverdb, err := sql.Open("singlestore", dsn)if err != nil {log.Fatal(err)}defer db.Close()// Verify connectionif err := db.Ping(); err != nil {log.Fatal(err)}fmt.Println("Connected to SingleStore")// Create tablecreateTableSQL := `CREATE TABLE IF NOT EXISTS users (id BIGINT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(100),email VARCHAR(255))`_, err = db.Exec(createTableSQL)if err != nil {log.Fatal(err)}fmt.Println("Table created")// Insert rowsinsertSQL := `INSERT INTO users (name, email)VALUES (?, ?)`_, err = db.Exec(insertSQL, "Alice", "alice@example.com")if err != nil {log.Fatal(err)}_, err = db.Exec(insertSQL, "Bob", "bob@example.com")if err != nil {log.Fatal(err)}fmt.Println("Rows inserted")// Update a rowupdateSQL := `UPDATE usersSET email = ?WHERE name = ?`result, err := db.Exec(updateSQL, "alice@newdomain.com", "Alice")if err != nil {log.Fatal(err)}rowsUpdated, _ := result.RowsAffected()fmt.Printf("Rows updated: %d\n", rowsUpdated)// Delete a rowdeleteSQL := `DELETE FROM usersWHERE name = ?`result, err = db.Exec(deleteSQL, "Bob")if err != nil {log.Fatal(err)}rowsDeleted, _ := result.RowsAffected()fmt.Printf("Rows deleted: %d\n", rowsDeleted)// Query remaining rowsrows, err := db.Query(`SELECT id, name, emailFROM users`)if err != nil {log.Fatal(err)}defer rows.Close()fmt.Println("\nRemaining users:")for rows.Next() {var id intvar name stringvar email stringif err := rows.Scan(&id, &name, &email); err != nil {log.Fatal(err)}fmt.Printf("ID=%d Name=%s Email=%s\n", id, name, email)}if err := rows.Err(); err != nil {log.Fatal(err)}} -
Run the application.
go run main.goConnected to SingleStore Table created Rows inserted Rows updated: 1 Rows deleted: 1 Remaining users: ID=1 Name=Alice Email=alice@newdomain.com
References
-
Go-SingleStore-Driver GitHub repository
-
Additional examples:
Last modified: