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:

Justin Kitagawa Leader Spotlight

Leader Spotlight: Riding the rocket ship of scale, with Justin Kitagawa

We sit down with Justin Kitagawa to learn more about his leadership style and approach for handling the complexities that come with scaling fast.

Jessica Srinivas
Mar 29, 2024 â‹… 8 min read
Nx Adoption Guide: Overview, Examples, And Alternatives

Nx adoption guide: Overview, examples, and alternatives

Let’s explore Nx features, use cases, alternatives, and more to help you assess whether it’s the right tool for your needs.

Andrew Evans
Mar 28, 2024 â‹… 9 min read
Understanding Security In React Native Applications

Understanding security in React Native applications

Explore the various security threats facing React Native mobile applications and how to mitigate them.

Wisdom Ekpotu
Mar 27, 2024 â‹… 10 min read
Warp Adoption Guide: Overview, Examples, And Alternatives

warp adoption guide: Overview, examples, and alternatives

The warp web framework for Rust offers many enticing features. Let’s see when and why you should consider using warp in your projects.

Ukeje Goodness
Mar 26, 2024 â‹… 8 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