Golang database/sql.DB type examples
package database/sql
DB is a database handle representing a pool of zero or more underlying connections. It's safe for concurrent use by multiple goroutines.
The sql package creates and frees connections automatically; it also maintains a free pool of idle connections. If the database has a concept of per-connection state, such state can only be reliably observed within a transaction. Once DB.Begin is called, the returned Tx is bound to a single connection. Once Commit or Rollback is called on the transaction, that transaction's connection is returned to DB's idle connection pool. The pool size can be controlled with SetMaxIdleConns.
Golang database/sql.DB type usage examples
Example 1:
func getServerVersion(t *testing.T, db *sql.DB) int {
var version int
err := db.QueryRow("SHOW server_version_num").Scan(&version)
if err != nil {
t.Fatal(err)
}
return version
}
Example 2:
// mysql session store
type MysqlSessionStore struct {
c *sql.DB
sid string
lock sync.RWMutex
values map[interface{}]interface{}
}
Reference :
Advertisement
Something interesting
Tutorials
+12.5k Golang : Forwarding a local port to a remote server example
+5k Golang : Calculate a pip value and distance to target profit example
+15.6k Golang : How to convert(cast) IP address to string?
+15k Golang : package is not in GOROOT during compilation
+46.5k Golang : Marshal and unmarshal json.RawMessage struct example
+12.7k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+22.2k Golang : How to run Golang application such as web server in the background or as daemon?
+18.8k Golang : Implement getters and setters
+8.7k Golang : How to join strings?
+9.3k Golang : Temperatures conversion example
+6.1k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+7.9k Javascript : How to check a browser's Do Not Track status?