Skip to content

SQL

Database management system that officially supported by Go's standard library. To use this in Gowok, you need to do configuration (read this page ).

In Gowok, you can connect to multiple SQL servers with different connection types. Every connection will identified by name. Connection name can be anything, as long as it easy to remember.

Example scenario with multiple SQL connections:

  • name: master, driver: mariadb
  • name: transaction, driver: postgres

Driver Setup

After configure SQL inside configuration file, you need to install the driver. It's easy, just import it.

go
// choose one or some as you want
import _ "github.com/go-sql-driver/mysql"
import _ "github.com/lib/pq"
import _ "github.com/mattn/go-sqlite3"

Then run this command:

bash
go mod tidy

Now, you ready to use SQL in your project.

Get Connection

SQL operations done by the connection. To get it, you can do this way:

go
import "github.com/gowok/gowok/sql"

sql.DB()

// or with name
sql.DB("master")
sql.DB("transactions")
  • If you get connection without define the name or you get unknown connection, Gowok will give you connection named default.
  • If it doesn't exist, Gowok will give you some.Some[sql.DB]{} (empty nil safety).

Since getting SQL connection gives you some.Some, you need to unwrap value by nil safety way .

Example:

go
var result int
err := sql.DB("master").OrPanic().QueryRow("SELECT 1").Scan(&result)
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(result)

You agree if it is easy, right? 😎

Released under the MIT License.