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
+6.4k Golang : Decode XML data from RSS feed
+4k Golang : Valued expressions and functions example
+50.6k Golang : Check if item is in slice/array
+9.5k Golang : Get escape characters \u form from unicode characters
+16.8k Golang : Clone with pointer and modify value
+12.2k Golang : Pass database connection to function called from another package and HTTP Handler
+15.5k Golang : Find out mime type from bytes in buffer
+13.2k Golang : How to check if a file is hidden?
+12.9k Golang : Strings comparison
+7.3k Golang : get the current working directory of a running program
+25.1k Golang : missing Mercurial command
+28.9k Golang : Get time.Duration in year, month, week or day