13. SQL and Databases
Activity 13.1: Holding User Data in a Table
Solution:
- Initialize your script with the appropriate imports. Let's call it
main.go
. Prepare an emptymain()
function:package main import "fmt" import "database/sql" import _ "github.com/lib/pq" func main(){ }
- Let's define the
struct
that will hold the users:type Users struct {     id int     name string     email string }
- Now the time has come to create two users:
users := []Users{ Â Â {1,"Szabo Daniel","daniel@packt.com"}, Â Â {2,"Szabo Florian","florian@packt.com"}, }
- Let's open the connection to our
Postgres
server:db, err := sql.Open("postgres", "user=postgres password=Start!123 host=127.0.0.1 port=5432 dbname=postgres sslmode=disable") if err != nil { Â Â panic(err) }else{ Â Â fmt.Println("...