Skip to content

Latest commit

 

History

History

101-with-golang

Using PostgreSql with Golang

Complete Intro To SQL by Brian Holt, but using Golang, sqlx and sql-migrate

Setup

Running Migrations

Install sql-migrate

(Assuming the database dirty exists)

~/go/bin/sql-migrate up -config=migrations-config.yml

To create a new migration

~/go/bin/sql-migrate new -config=migrations-config.yml <name>

To undo the last migration

~/go/bin/sql-migrate down -config=migrations-config.yml

or to undo all migrations

~/go/bin/sql-migrate down -config=migrations-config.yml -limit=0

Running Go Scripts

go run main.go

Live Reload

Install Gin

go install github.com/codegangsta/gin
~/go/bin/gin --bin bin/gin-bin --appPort 8080 --immediate run .

API Calls

curl --location --request GET 'localhost:8080/ingredients/search?term=a&page=1'
curl --location --request GET 'localhost:8080/ingredients/type?type=fruit'

curl --location 'localhost:8080/recipes/search'
curl --location 'localhost:8080/recipes/get?id=1'

Useful Links

  1. Connection
  2. https://www.alexedwards.net/blog/serving-static-sites-with-go
  3. Guide to SQLx
  4. Code for the SQL apps project
  5. Intro to Go by Brenna Martenson
  6. PostgreSql Window function
  7. https://stackoverflow.com/questions/18635671/how-to-define-multiple-name-tags-in-a-struct
  8. https://stackoverflow.com/questions/12334697/variadic-functions-parameters-pass-through
  9. https://stackoverflow.com/questions/8270816/converting-go-struct-to-json
  10. https://www.postgresql.org/docs/current/sql-explain.html
  11. https://stackoverflow.com/questions/56244336/go-package-with-multiple-files-how-to-structure