Skip to content

Instantly share code, notes, and snippets.

View gufranmirza's full-sized avatar

Gufran Mirza gufranmirza

View GitHub Profile
<!DOCTYPE html>
<html lang="en">
<head>
<title>The bird encyclopedia</title>
</head>
<body>
<h1>The bird encyclopedia</h1>
<!--
func getBirdHandler(w http.ResponseWriter, r *http.Request) {
//Convert the "birds" variable to json
birdListBytes, err := json.Marshal(birds)
// If there is an error, print it to the console, and return a server
// error response to the user
if err != nil {
fmt.Println(fmt.Errorf("Error: %v", err))
w.WriteHeader(http.StatusInternalServerError)
return
func createBirdHandler(w http.ResponseWriter, r *http.Request) {
// Create a new instance of Bird
bird := Bird{}
// We send all our data as HTML form data
// the `ParseForm` method of the request, parses the
// form values
err := r.ParseForm()
// In case of any error, we respond with an error to the user
package main
// The sql go library is needed to interact with the database
import (
"database/sql"
)
// Our store will have two methods, to add a new bird,
// and to get all existing birds
// Each method returns an error, in case something goes wrong
package main
import (
"database/sql"
"testing"
// The "testify/suite" package is used to make the test suite
"github.com/stretchr/testify/suite"
)
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Bird struct {
Species string `json:"species"`
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Bird struct {
Species string `json:"species"`
package main
import (
"github.com/stretchr/testify/mock"
)
// The mock store contains additonal methods for inspection
type MockStore struct {
mock.Mock
}
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"testing"
import (
//...
// The libn/pq driver is used for postgres
_ "github.com/lib/pq"
//...
)
func main(){
// ...
connString := "dbname=<your main db name> sslmode=disable"