func getAllBooks() ([]Book, error) {
	books := []Book{}
	rows, err := db.Query(`SELECT id, name, author, pages, publication_date FROM books order by id`)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	for rows.Next() {
		var id int
		var name string
		var author string
		var pages int
		var publicationDate pq.NullTime
		err = rows.Scan(&id, &name, &author, &pages, &publicationDate)
		if err != nil {
			return books, err
		}
		currentBook := Book{ID: id, Name: name, Author: author, Pages: pages}
		if publicationDate.Valid {
			currentBook.PublicationDate = publicationDate.Time
		}
		books = append(books, currentBook)
	}
	return books, err
}
Get all books from the database
package main
import (
	"database/sql"
	"fmt"
	"log"
	"time"
	"github.com/lib/pq"
	_ "github.com/lib/pq"
)
const (
	host     = "localhost"
	port     = 5432
	user     = "postgres"
	password = "example"
	dbname   = "pq-demo"
)
type Book struct {
	ID              int
	Name            string
	Author          string
	PublicationDate time.Time
	Pages           int
}
func createTable() {
	query := `
	CREATE TABLE books
	(
	 id serial NOT NULL,
	 name character varying NOT NULL,
	 author character varying,
	 pages integer,
	 publication_date date,
	 CONSTRAINT pk_books PRIMARY KEY (id )
	)
	WITH (
	 OIDS=FALSE
	);
	ALTER TABLE books
	 OWNER TO postgres;`
	if _, err := db.Exec(query); err != nil {
		log.Fatal(err)
	}
}
func dropTable() {
	query := `DROP TABLE books;`
	if _, err := db.Exec(query); err != nil {
		log.Fatal(err)
	}
}
func getBook(bookID int) (Book, error) {
	res := Book{}
	var id int
	var name string
	var author string
	var pages int
	var publicationDate pq.NullTime
	err := db.QueryRow(`SELECT id, name, author, pages, publication_date FROM books where id = $1`, bookID).Scan(&id, &name, &author, &pages, &publicationDate)
	if err == nil {
		res = Book{ID: id, Name: name, Author: author, Pages: pages, PublicationDate: publicationDate.Time}
	}
	return res, err
}