Important

Helios features are now enabled during weekly update windows and are no longer directly tied to SingleStore engine releases. Refer to the release notes to view the latest features available in your Helios cluster.

Connect with Go

Use the Go SingleStore driver (go-singlestore-driver), a Go driver for the database/sql driver package, to connect Go applications to SingleStore databases. This driver includes features and improvements specific to SingleStore.

Refer to the Go-SingleStore-Driver GitHub repository for more information on supported features and enhancements.

Prerequisites

  • Install the latest version of Go.

  • SingleStore 8.7 or later.

  • (Optional) Install git.

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 tcp protocol 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 cluster.

  • 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.

  1. Create a project folder and initialize the Go module.

    mkdir ssGo
    cd ssGo
    go mod init ssGo
  2. Install the go-singlestore-driver package.

    go get -u github.com/singlestore-labs/go-singlestore-driver/v2
  3. 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 main
    import (
    "database/sql"
    "fmt"
    "log"
    _ "github.com/singlestore-labs/go-singlestore-driver/v2"
    )
    func main() {
    // Connection settings
    host := "svc-xxxx"
    port := "3306"
    user := "s2user"
    password := "p455w1kd"
    database := "demo"
    // SingleStore DSN
    dsn := fmt.Sprintf(
    "%s:%s@tcp(%s:%s)/%s?parseTime=true",
    user,
    password,
    host,
    port,
    database,
    )
    // Open connection using the SingleStore driver
    db, err := sql.Open("singlestore", dsn)
    if err != nil {
    log.Fatal(err)
    }
    defer db.Close()
    // Verify connection
    if err := db.Ping(); err != nil {
    log.Fatal(err)
    }
    fmt.Println("Connected to SingleStore")
    // Create table
    createTableSQL := `
    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 rows
    insertSQL := `
    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 row
    updateSQL := `
    UPDATE users
    SET 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 row
    deleteSQL := `
    DELETE FROM users
    WHERE 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 rows
    rows, err := db.Query(`
    SELECT id, name, email
    FROM users
    `)
    if err != nil {
    log.Fatal(err)
    }
    defer rows.Close()
    fmt.Println("\nRemaining users:")
    for rows.Next() {
    var id int
    var name string
    var email string
    if 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)
    }
    }
  4. Run the application.

    go run main.go
    Connected to SingleStore
    Table created
    Rows inserted
    Rows updated: 1
    Rows deleted: 1
    
    Remaining users:
    ID=1 Name=Alice Email=alice@newdomain.com

References

Last modified:

Was this article helpful?