Operations with prepared statements
Prepared statements bring security, efficiency, and convenience. Naturally, it is possible to use them with the Go standard library; this recipe will show how.
Getting ready
Verify if Go is properly installed by calling the go version
command in your Terminal. If the command fails, follow the Getting ready section in the first recipe of this chapter.
Set up the PostgreSQL server, as mentioned in the first recipe of this chapter.
How to do it...
- Run the following SQL script against your sample database:
DROP TABLE IF EXISTS post; CREATE TABLE post ( ID serial, TITLE varchar(40), CONTENT varchar(255), CONSTRAINT pk_post PRIMARY KEY(ID) ); SELECT * FROM post;
- Open the console and create the folder
chapter08/recipe04
. - Navigate to the directory.
- Create the
prepared.go
file with the following content:
package main import ( "database/sql" "fmt" _ "github...