Skip to content

Commit

Permalink
Update README to mention the New function
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Apr 15, 2021
1 parent 6581d91 commit aada3fd
Showing 1 changed file with 45 additions and 22 deletions.
67 changes: 45 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,56 @@ Given, you have a mysql database called **txdb_test** and a table **users** with
column.

``` go
package main
package main

import (
"database/sql"
"log"

"github.com/DATA-DOG/go-txdb"
_ "github.com/go-sql-driver/mysql"
)

func init() {
// we register an sql driver named "txdb"
txdb.Register("txdb", "mysql", "root@/txdb_test")
}

func main() {
// dsn serves as an unique identifier for connection pool
db, err := sql.Open("txdb", "identifier")
if err != nil {
log.Fatal(err)
}
defer db.Close()

import (
"database/sql"
"log"
if _, err := db.Exec(`INSERT INTO users(username) VALUES("gopher")`); err != nil {
log.Fatal(err)
}
}
```

"github.com/DATA-DOG/go-txdb"
_ "github.com/go-sql-driver/mysql"
)
You can also use [`sql.OpenDB`](https://golang.org/pkg/database/sql/#OpenDB) (added in Go 1.10) rather than registering a txdb driver instance, if you prefer:

func init() {
// we register an sql driver named "txdb"
txdb.Register("txdb", "mysql", "root@/txdb_test")
}
``` go
package main

import (
"database/sql"
"log"

"github.com/DATA-DOG/go-txdb"
_ "github.com/go-sql-driver/mysql"
)

func main() {
db := sql.OpenDB(txdb.New("mysql", "root@/txdb_test"))
defer db.Close()

func main() {
// dsn serves as an unique identifier for connection pool
db, err := sql.Open("txdb", "identifier")
if err != nil {
log.Fatal(err)
}
defer db.Close()

if _, err := db.Exec(`INSERT INTO users(username) VALUES("gopher")`); err != nil {
log.Fatal(err)
}
if _, err := db.Exec(`INSERT INTO users(username) VALUES("gopher")`); err != nil {
log.Fatal(err)
}
}
```

Every time you will run this application, it will remain in the same state as before.
Expand Down

0 comments on commit aada3fd

Please sign in to comment.