- Fetch and install the Compile Daemon
https://github.com/githubnemo/CompileDaemon
go get github.com/githubnemo/CompileDaemon
go install github.com/githubnemo/CompileDaemon
- Fetch GoDotEnv for environment variables
https://github.com/joho/godotenv
go get github.com/joho/godotenv
- Add Gin web framework
https://github.com/gin-gonic/gin
go get -u github.com/gin-gonic/gin
- Download and include GORM
go get -u gorm.io/gorm
- Add a
main.go
and run the Compile Daemon
cat main.go
package main
import "fmt"
func main() {
fmt.Println("Hello world")
}
And in the same directory:
CompileDaemon -command="./go-gin-gorm-crud"
And it will auto build upon saved changes
- Setup
.env
file
echo PORT=3000 > .env
cat .env
PORT=3000
DB_INFO="host=localhost user=heyjdp password=password123 dbname=go_crud_demo port=5432 sslmode=disable TimeZone=Asia/Nicosia"
And see usage examples here: https://github.com/joho/godotenv
- Setup postgres
NOTE: Assuming you are on Debian Linux and installed via apt
First, create the role by issuing the following command:
sudo su - postgres -c "createuser heyjdp"
And make a password for the user:
sudo -u postgres psql -c "ALTER USER heyjdp PASSWORD 'password123';"
Next, create the database using the createdb command:
sudo su - postgres -c "createdb go_crud_demo"
To grant permissions to the user on the database, connect to the PostgreSQL shell:
sudo -u postgres psql
Run the following query:
GRANT ALL PRIVILEGES ON DATABASE go_crud_demo TO heyjdp;
And use \q
to quit the postgres shell
NOTE: By default postgres is listening on port 5432 and is limited to connections from localhost. This is a good thing if you are going to make a dumbass password like above ;)
ss -nlt | grep 5432
LISTEN 0 128 127.0.0.1:5432 0.0.0.0:*
LISTEN 0 128 [::1]:5432 [::]:*
- Migrate the models to the DB
go run migrate/migrate.go
And check the DB tables:
sudo -u postgres psql
postgres=# \c go_crud_demo \d
You are now connected to database "go_crud_demo" as user "postgres".
List of relations
Schema | Name | Type | Owner
--------+--------------+----------+--------
public | posts | table | heyjdp
public | posts_id_seq | sequence | heyjdp
(2 rows)
NOTE: GUI software such as DBeaver (https://dbeaver.io/) or pgAdmin (https://www.pgadmin.org/https://www.pgadmin.org/) will do the same job