Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
æ 
Building Apps with Go & MySQL 
1 
VividCortex Webinar - Baron Schwartz - November 11, 2014
å 
About Baron 
Founder of @VividCortex 
Author of High Performance MySQL 
@xaprb on Twitter 
baron@vividcortex.com 
http...
æ 
VividCortex - MySQL Performance Management 
3
å 
What’s Nice About Go? 
4 
Interfaces 
Concurrency primitives 
Syntax 
Performance 
Static Linking 
Tools
å 
What’s Not As Nice? 
Hard to Google it (use “golang”) 
Package versioning (use github.com/VividCortex/johnny-deps/) 
D...
æ 
Go Database Design Patterns 
6
å 
Things To Know 
What is database/sql? 
Where is the documentation? 
How do I create a database connection? 
How do I q...
å 
Overview of database/sql 
A generic, minimal interface for SQL-like databases 
Verbs and nouns: 
Open, Prepare, Exec, ...
package 
main 
æ 
import 
( 
9 
_ 
"github.com/go-­‐sql-­‐driver/mysql" 
"database/sql" 
"log" 
) 
func 
main() 
{ 
db, 
...
å One-Shot Query 
10 
var 
str 
string 
err 
= 
db.QueryRow( 
"select 
hello 
from 
hello.world 
where 
id 
= 
1").Scan(&...
å Query... 
11 
var 
id 
int 
var 
str 
string 
q 
:= 
"select 
id, 
hello 
from 
hello.world 
where 
id 
= 
?" 
rows, 
e...
å ... Then Fetch Rows 
12 
for 
rows.Next() 
{ 
err 
= 
rows.Scan(&id, 
&str) 
if 
err 
!= 
nil 
{ 
log.Fatal(err) 
} 
//...
å Prepare... 
13 
stmt, 
err 
:= 
db.Prepare( 
"select 
id, 
hello 
from 
hello.world 
where 
id 
= 
?") 
if 
err 
!= 
ni...
å ... Then Execute & Fetch 
14 
rows, 
err 
:= 
stmt.Query(1) 
if 
err 
!= 
nil 
{ 
log.Fatal(err) 
} 
defer 
rows.Close(...
å Insert... 
15 
stmt, 
err 
:= 
db.Prepare( 
"insert 
into 
hello.world(hello) 
values(?)") 
if 
err 
!= 
nil 
{ 
log.Fa...
å ... And Check Results 
16 
lastId, 
err 
:= 
res.LastInsertId() 
if 
err 
!= 
nil 
{ 
log.Fatal(err) 
} 
rowCnt, 
err 
...
æ 
Docs: “Pleasantly Lightweight!” 
17
æ 
Connection Pooling 
18
æ 
Big Unsigned Integers 
19
æ 
Unexpected Connections 
20
å Resource Exhaustion 
21 
// 
Don’t 
do 
this! 
for 
i 
:= 
0; 
i 
< 
50; 
i++ 
{ 
_, 
err 
:= 
db.Query("DELETE 
FROM 
...
æ 
Retry. Retry. Retry. Retry. Retry. 
Retry. Retry. Retry. Retry. Retry. 
22
æ 
Handling NULLs 
23
å Working with NULL 
24 
var 
s 
sql.NullString 
err 
:= 
db.QueryRow( 
"SELECT 
name 
FROM 
foo 
WHERE 
id=?", 
id).Scan...
å 
Boilerplate code everywhere 
There’s no sql.NullUint64 
Nullability is tricky, not future-proof 
It’s not very Go-ish ...
æ 
ORMs - Not Really Suited for Go 
26
æ 
Interfaces - Learn Them, Love Them 
27 
e.g. https://vividcortex.com/blog/2014/11/11/encrypting-data-in-mysql-with-go/
å 
Resources 
28 
http://golang.org/pkg/database/sql/ 
http://go-database-sql.org/ 
https://github.com/go-sql-driver/mysq...
æ 
Questions? 
baron@vividcortex.com 
@xaprb • linkedin.com/in/xaprb
å 
Image Credits 
http://www.flickr.com/photos/simens/6306917636/ 
http://www.flickr.com/photos/dexxus/5794905716/ 
http:...
Upcoming SlideShare
Loading in …5
×

Using MySQL with Go - Golang's Database/SQL Driver

18,251 views

Published on

Google’s Go language is increasingly popular for systems programming. It’s efficient and fun to program in Go, and it produces high-performance programs. Go is well suited for working with SQL databases, and has excellent MySQL drivers. In this talk Baron will show how to use Go’s database/sql package with MySQL. He’ll discuss idiomatic database/sql code, available drivers for MySQL, and show tips and tricks that will save you time and frustration.

Published in: Software
  • Be the first to comment

Using MySQL with Go - Golang's Database/SQL Driver

  1. 1. æ Building Apps with Go & MySQL 1 VividCortex Webinar - Baron Schwartz - November 11, 2014
  2. 2. å About Baron Founder of @VividCortex Author of High Performance MySQL @xaprb on Twitter baron@vividcortex.com http://www.linkedin.com/in/xaprb Optimization, Backups, Replication, and more 3rd Edition Covers Version 5.5 High Performance MySQL Baron Schwartz, Peter Zaitsev & Vadim Tkachenko 2
  3. 3. æ VividCortex - MySQL Performance Management 3
  4. 4. å What’s Nice About Go? 4 Interfaces Concurrency primitives Syntax Performance Static Linking Tools
  5. 5. å What’s Not As Nice? Hard to Google it (use “golang”) Package versioning (use github.com/VividCortex/johnny-deps/) Doesn’t have generics (not a big deal) 5
  6. 6. æ Go Database Design Patterns 6
  7. 7. å Things To Know What is database/sql? Where is the documentation? How do I create a database connection? How do I query a connection? How do I retrieve results from a query? What are the common operations? 7
  8. 8. å Overview of database/sql A generic, minimal interface for SQL-like databases Verbs and nouns: Open, Prepare, Exec, Query, QueryRow, Scan, Close DB, Stmt, Value, Row, Rows, Result Tx, Begin, Commit, Rollback Docs: http://golang.org/pkg/database/sql/ Tutorial: http://go-database-sql.org/ 8
  9. 9. package main æ import ( 9 _ "github.com/go-­‐sql-­‐driver/mysql" "database/sql" "log" ) func main() { db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/test") if err != nil { log.Println(err) } defer db.Close() // Do something useful here. }
  10. 10. å One-Shot Query 10 var str string err = db.QueryRow( "select hello from hello.world where id = 1").Scan(&str) if err != nil && err != sql.ErrNoRows { log.Println(err) } log.Println(str)
  11. 11. å Query... 11 var id int var str string q := "select id, hello from hello.world where id = ?" rows, err := db.Query(q, 1) if err != nil { log.Fatal(err) } defer rows.Close()
  12. 12. å ... Then Fetch Rows 12 for rows.Next() { err = rows.Scan(&id, &str) if err != nil { log.Fatal(err) } // Use the variables scanned from the row } if err = rows.Err(); err != nil { log.Fatal(err) }
  13. 13. å Prepare... 13 stmt, err := db.Prepare( "select id, hello from hello.world where id = ?") if err != nil { log.Fatal(err) } defer stmt.Close()
  14. 14. å ... Then Execute & Fetch 14 rows, err := stmt.Query(1) if err != nil { log.Fatal(err) } defer rows.Close() for rows.Next() { // ... }
  15. 15. å Insert... 15 stmt, err := db.Prepare( "insert into hello.world(hello) values(?)") if err != nil { log.Fatal(err) } defer stmt.Close() res, err := stmt.Exec("hello, Dolly") if err != nil { log.Fatal(err) }
  16. 16. å ... And Check Results 16 lastId, err := res.LastInsertId() if err != nil { log.Fatal(err) } rowCnt, err := res.RowsAffected() if err != nil { log.Fatal(err) }
  17. 17. æ Docs: “Pleasantly Lightweight!” 17
  18. 18. æ Connection Pooling 18
  19. 19. æ Big Unsigned Integers 19
  20. 20. æ Unexpected Connections 20
  21. 21. å Resource Exhaustion 21 // Don’t do this! for i := 0; i < 50; i++ { _, err := db.Query("DELETE FROM hello.world") } // Use this instead! for i := 0; i < 50; i++ { _, err := db.Exec("DELETE FROM hello.world") }
  22. 22. æ Retry. Retry. Retry. Retry. Retry. Retry. Retry. Retry. Retry. Retry. 22
  23. 23. æ Handling NULLs 23
  24. 24. å Working with NULL 24 var s sql.NullString err := db.QueryRow( "SELECT name FROM foo WHERE id=?", id).Scan(&s) if s.Valid { // use s.String } else { // NULL value } // If you don’t care to check whether it was NULL, // Just use s.String without checking, it’ll be ""
  25. 25. å Boilerplate code everywhere There’s no sql.NullUint64 Nullability is tricky, not future-proof It’s not very Go-ish (no useful default zero-value) 25 NULL Makes Code Ugly
  26. 26. æ ORMs - Not Really Suited for Go 26
  27. 27. æ Interfaces - Learn Them, Love Them 27 e.g. https://vividcortex.com/blog/2014/11/11/encrypting-data-in-mysql-with-go/
  28. 28. å Resources 28 http://golang.org/pkg/database/sql/ http://go-database-sql.org/ https://github.com/go-sql-driver/mysql http://jmoiron.net/blog/ http://twitter.com/VividCortex/ <--- lots of Go tips
  29. 29. æ Questions? baron@vividcortex.com @xaprb • linkedin.com/in/xaprb
  30. 30. å Image Credits http://www.flickr.com/photos/simens/6306917636/ http://www.flickr.com/photos/dexxus/5794905716/ http://www.flickr.com/photos/sebastian_bergmann/202396633/ http://www.flickr.com/photos/doug88888/4794114114/ http://www.flickr.com/photos/oatsy40/6443878013/ http://www.sxc.hu/photo/1160562/ Google Maps (screenshot) http://www.flickr.com/photos/estherase/13553883/ http://www.flickr.com/photos/paperpariah/4150220583/ http://www.flickr.com/photos/zooboing/4743616313/ http://www.flickr.com/photos/dseneste/5912382808/ http://www.flickr.com/photos/clickykbd/66165381/sizes/l/ http://www.flickr.com/photos/mamnaimie/5576980406/ https://www.flickr.com/photos/zachstern/87431231 30

×