These steps cover writing and running your application:
- From your Terminal or console application, create a new directory called ~/projects/go-programming-cookbook/chapter6/database and navigate to this directory.
- Run the following command:
$ go mod init github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter6/database
You should see a file called go.mod that containing the following:
module github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter6/database
- Copy tests from ~/projects/go-programming-cookbook-original/chapter6/database, or use this as an exercise to write some code of your own!
- Create a file called config.go with the following content:
package database
import (
"database/sql"
"fmt"
"os"
"time"
_ "github.com/go-sql-driver/mysql" //we import supported
libraries for database/sql
)
// Example hold the results of our queries
type Example struct {
Name string
Created *time.Time
}
// Setup configures and returns our database
// connection poold
func Setup() (*sql.DB, error) {
db, err := sql.Open("mysql",
fmt.Sprintf("%s:%s@/gocookbook?
parseTime=true", os.Getenv("MYSQLUSERNAME"),
os.Getenv("MYSQLPASSWORD")))
if err != nil {
return nil, err
}
return db, nil
}
- Create a file called create.go with the following content:
package database
import (
"database/sql"
_ "github.com/go-sql-driver/mysql" //we import supported
libraries for database/sql
)
// Create makes a table called example
// and populates it
func Create(db *sql.DB) error {
// create the database
if _, err := db.Exec("CREATE TABLE example (name
VARCHAR(20), created DATETIME)"); err != nil {
return err
}
if _, err := db.Exec(`INSERT INTO example (name, created)
values ("Aaron", NOW())`); err != nil {
return err
}
return nil
}
- Create a file called query.go with the following content:
package database
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql" //we import supported
libraries for database/sql
)
// Query grabs a new connection
// creates tables, and later drops them
// and issues some queries
func Query(db *sql.DB, name string) error {
name := "Aaron"
rows, err := db.Query("SELECT name, created FROM example
where name=?", name)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var e Example
if err := rows.Scan(&e.Name, &e.Created); err != nil {
return err
}
fmt.Printf("Results:\n\tName: %s\n\tCreated: %v\n",
e.Name, e.Created)
}
return rows.Err()
}
- Create a file called exec.go with the following content:
package database
// Exec replaces the Exec from the previous
// recipe
func Exec(db DB) error {
// uncaught error on cleanup, but we always
// want to cleanup
defer db.Exec("DROP TABLE example")
if err := Create(db); err != nil {
return err
}
if err := Query(db, "Aaron"); err != nil {
return err
}
return nil
}
- Create and navigate to the example directory.
- Create a file called main.go with the following content:
package main
import (
"PacktPublishing/Go-Programming-Cookbook-Second-Edition/
go-cookbook/chapter6/database"
_ "github.com/go-sql-driver/mysql" //we import supported
libraries for database/sql
)
func main() {
db, err := database.Setup()
if err != nil {
panic(err)
}
if err := database.Exec(db); err != nil {
panic(err)
}
}
- Run go run main.go.
- You could also run the following command:
$ go build
$ ./example
You should see the following output:
$ go run main.go
Results:
Name: Aaron
Created: 2017-02-16 19:02:36 +0000 UTC
- The go.mod file may be updated and go.sum file should now be present in the top-level recipe directory.
- If you copied or wrote your own tests, go up one directory and run go test. Ensure that all the tests pass.