Last active
October 15, 2025 17:51
-
-
Save alexedwards/dc3145c8e2e6d2fd6cd9 to your computer and use it in GitHub Desktop.
Revisions
-
alexedwards revised this gist
Jun 13, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,6 +19,7 @@ func main() { if err != nil { log.Fatal(err) } defer db.Close() rows, err := db.Query("SELECT * FROM books") if err != nil { -
alexedwards revised this gist
Jun 13, 2015 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -48,4 +48,10 @@ func main() { } fmt.Printf("%s, %s, %s, %s\n", bk.Isbn, bk.Title.String, bk.Author.String, price) } } // Should output: // 978-1503261969, Emma, Jayne Austen, £9.44 // 978-1514274873, Journal of a Soldier, , £5.49 // 978-1503379640, The Prince, Niccolò Machiavelli, PRICE NOT SET -
alexedwards created this gist
Jun 13, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ CREATE TABLE books ( isbn char(14) NOT NULL, title varchar(255), author varchar(255), price decimal(5,2) ); INSERT INTO books (isbn, title, author, price) VALUES ('978-1503261969', 'Emma', 'Jayne Austen', 9.44), ('978-1514274873', 'Journal of a Soldier', NULL, 5.49), ('978-1503379640', 'The Prince', 'Niccolò Machiavelli', NULL); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ package main import ( _ "github.com/lib/pq" "database/sql" "fmt" "log" ) type Book struct { Isbn string Title sql.NullString Author sql.NullString Price sql.NullFloat64 } func main() { db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore") if err != nil { log.Fatal(err) } rows, err := db.Query("SELECT * FROM books") if err != nil { log.Fatal(err) } defer rows.Close() bks := make([]*Book, 0) for rows.Next() { bk := new(Book) err := rows.Scan(&bk.Isbn, &bk.Title, &bk.Author, &bk.Price) if err != nil { log.Fatal(err) } bks = append(bks, bk) } if err = rows.Err(); err != nil { log.Fatal(err) } for _, bk := range bks { var price string if bk.Price.Valid { price = fmt.Sprintf("£%.2f", bk.Price.Float64) } else { price = "PRICE NOT SET" } fmt.Printf("%s, %s, %s, %s\n", bk.Isbn, bk.Title.String, bk.Author.String, price) } }