Golang and Gin Restful API

Ref: https://santrikoding.com/tutorial-restful-api-golang-1-membuat-project-golang

Note: Make sure golang is already installed by checking with ‘go version’ on your linux terminal
[codesyntax lang=”bash”]

[/codesyntax]
Note: it’ll be better to use the newest go version. I already installed go version 1.23.4
[codesyntax lang=”bash”]

[/codesyntax]

Note: don’t forget to run ‘go mod tidy’ on your go project

1. Create a new directory for this project named ‘go-restful-api’ then go into the directory
[codesyntax lang=”bash”]

[/codesyntax]

then type this to create a new module ‘advcha/backend-api’
[codesyntax lang=”bash”]

[/codesyntax]

2. Install ‘Gin’ and the libraries. Gin is a go-based web framework
[codesyntax lang=”bash”]

[/codesyntax]

3. Create a ‘Hello World’ with Gin
create a new file ‘main.go’ then here is the content
[codesyntax lang=”text”]

[/codesyntax]

4. Run it with ‘go run main.go’
[codesyntax lang=”bash”]

[/codesyntax]

5. open it on your browser with http://localhost:3000/
it should print

Install GORM (Go ORM). it’s a Object Relational Mapping (ORM) for Golang
1. Install GORM with MySQL driver
[codesyntax lang=”bash”]

[/codesyntax]

2. Create a new .env file to store the variables like the database name, password, etc.
[codesyntax lang=”text”]

[/codesyntax]
Note: rename ‘your_mysql_username’ and ‘your_mysql_password’ for your mysql account
‘db_go_restful_api’ is the database name we will create later

We need to install a package ‘github.com/joho/godotenv’ on the terminal ‘go get github.com/joho/godotenv’ to read the .env file
Note: run ‘go mod tidy’ to install any missing packages if exist

2. Create a Post model
create a new directory ‘models’ then create a new file ‘post.go’ in it. here is the file content
[codesyntax lang=”text”]

 

[/codesyntax]

3. create a database connection
create a new file ‘setup.go’ in the ‘models’ directory. here is the file content
[codesyntax lang=”text”]

[/codesyntax]

4. create a new mysql database ‘db_go_restful_api’

5. Create a new Post controller ‘postController.go’ in ‘controllers’ directory
[codesyntax lang=”text”]

[/codesyntax]

6. Create a route API Post
edit main.go to import the models and controllers. Also put the route in it. so it’ll be like this
[codesyntax lang=”text”]

[/codesyntax]

 

7. Run it and open it on your browser
If everything okay, the url http://localhost:3000/api/posts will show

Note: Also we can check it via Postman
create a new collection ‘Go Gin Restful API’ then create a new GET request ‘api_posts’ with url http://localhost:3000/api/posts

Insert data into the database
1. Create a new function ‘InsertPost’ in postController.go
note: first, we need to install a package ‘github.com/go-playground/validator/v10′[codesyntax lang=”bash”]

[/codesyntax]

[codesyntax lang=”text”]

[/codesyntax]

2. Then add a route in main.go to insert the post like this
[codesyntax lang=”text”]

[/codesyntax]

3. Run and test with Postman

Note: also need to test the validation with a json for empty title and content to make sure the validation is working.
the above screen showed the json with title and content and the success result
the output on http://localhost:3000/api/posts will be like this:

Leave a Reply

Your email address will not be published. Required fields are marked *