2022-11-10
2648
#go
Rahman Fadhil
14400
Nov 10, 2022 â‹… 9 min read

Building a REST API with Golang using Gin and Gorm

Rahman Fadhil Developer and content writer.

Recent posts:

Integrating Django Templates With React For Dynamic Webpages

Integrating Django templates with React for dynamic webpages

Create a dynamic demo blog site using Django and React to demonstrate Django’s server-side functionalities and React’s interactive UI.

Kayode Adeniyi
Apr 18, 2024 â‹… 7 min read
Using Aoi Js To Build A Bot For Discord

Using aoi.js to build a bot on Discord

Explore how the aoi.js library makes it easy to create Discord bots with useful functionalities for frontend applications.

Rahul Padalkar
Apr 17, 2024 â‹… 9 min read
Web Components Adoption Guide: Overview, Examples, And Alternatives

Web Components adoption guide: Overview, examples, and alternatives

Evaluate Web Components, a set of standards that allow you to create custom HTML tags for more reusable, manageable code.

Elijah Asaolu
Apr 16, 2024 â‹… 11 min read
Using Aws Lambda And Aws Cloudfront To Optimize Image Handling

Using AWS Lambda and CloudFront to optimize image handling

Leverage services like AWS Lambda, CloudFront, and S3 to handle images more effectively, optimizing performance and providing a better UX.

Nitish Sharma
Apr 12, 2024 â‹… 12 min read
View all posts

23 Replies to "Building a REST API with Golang using Gin and Gorm"

  1. Don’t use this on production if you don’t want to be hacked. Passing db through context is a really good idea(!)

  2. Thanks for the Article.. Very valuable..
    Some issues I found are
    1. Importing `gorm` package to books model throws “imported and not used” error.
    2. Since the main function has changed to a route handler, there is no need to import “net/http” package and will throw the same above error.
    3. The `delete` router seems to be missing controller action.

  3. If you want to improve something you need to share the work around as well, not just say the existing stuff is bad.

  4. For your first issue, you can add an underscore alias in front of the gorm import:

    “`
    import _ “github.com/jinzhu/gorm”

    “`

    One would do this to import “side effects” (static reference) of a module

  5. I found a reflect error when using the UpdateBook method it is because the types are not the same, since we are using an UpdateBookInput struct to update a Book struct. If helps someone in the same situation this is the code i changed:

    Started like this:

    models.DB.Model(&book).Updates(input)

    Changed it to this:

    models.DB.Model(&book).Updates(models.Book{Title: input.Title, Author: input.Author})

  6. Thanks, great article.
    May you give some example about relation like one to one, one to many, many to many and etc?

  7. Thanks a lot for your good article.
    Why do you use call by reference somewhere but use call by value another where?
    e.g.
    1. input:
    models.DB.Model(&book).Updates(input)

    2. book
    models.DB.Create(&book)

  8. models.DB.Model(&book).Updates(input)
    this caused error to me, I have to use json.Marshal(input) as replacement for input.
    In addition, I use gorm.io/gorm and gorm.io/driver/sqlite, and have to modify the database setup to
    database, err := gorm.Open(sqlite.Open(“test.db”))

Leave a Reply