REST API built using Golang, Gin and GORM with MySQL.
Requirements: Golang installed, and a MySQL database to connect to.
- Create a database/schema in MySQL.
- In the .env file as shown below, modify the
DB_URL
accordingly based on your MySQL Credentials and name of schema/db you just created. Can also changePORT
if required.PORT=3000 DB_URL=user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local
- For example, I used a MySQL schema with dbname: test123, user: root, pass: root, so my DB_URL was
root:root@tcp(127.0.0.1:3306)/test123?charset=utf8mb4&parseTime=True&loc=Local
. - In folder root, run
go run main.go
to auto download dependencies. - In folder root, open another terminal/CLI instance, run
go run migrate/migrate.go
to create database tables. - API ready to be used.
main_test.go
which contains the unit tests does not work. Need to learn how to do proper unit tests for GORM (maybe using SqlMock?).