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","[email protected]"}, {2,"Szabo Florian","[email protected]"}, }
- 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("...