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. For example, unix or tcp.

  • hostname: IP address or hostname of the SingleStore cluster. Specified when the protocol is tcp.

  • port: Port of the SingleStore cluster. The default is 3306. Specified when the protocol is tcp.

  • unix_socket: Unix domain socket for local inter-process communication between the client and SingleStore. Specify the filesystem path to the socket file, including the filename and extension. Specified when the protocol is unix.

  • 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>@<protocol>(<hostname:port_or_socket>)/<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?