Skip to content

Use golang, Gin and GORM to make a CRUD blog example

Notifications You must be signed in to change notification settings

heyjdp/go-gin-gorm-crud

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CRUD app in golang using Gin and GORM

  1. Fetch and install the Compile Daemon

https://github.com/githubnemo/CompileDaemon

go get github.com/githubnemo/CompileDaemon
go install github.com/githubnemo/CompileDaemon
  1. Fetch GoDotEnv for environment variables

https://github.com/joho/godotenv

go get github.com/joho/godotenv
  1. Add Gin web framework

https://github.com/gin-gonic/gin

go get -u github.com/gin-gonic/gin
  1. Download and include GORM

https://gorm.io/

go get -u gorm.io/gorm
  1. 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

  1. 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

  1. 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                [::]:*  
  1. 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

About

Use golang, Gin and GORM to make a CRUD blog example

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages