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:

Comparing Mutative Vs Immer Vs Reducers For Data Handling In React

Comparing React state tools: Mutative vs. Immer vs. reducers

Mutative processes data with better performance than both Immer and native reducers. Let’s compare these data handling options in React.

Rashedul Alam
Apr 26, 2024 â‹… 7 min read
Radix Ui Adoption Guide Overview Examples And Alternatives

Radix UI adoption guide: Overview, examples, and alternatives

Radix UI is quickly rising in popularity and has become an excellent go-to solution for building modern design systems and websites.

Nefe Emadamerho-Atori
Apr 25, 2024 â‹… 11 min read
Understanding The Css Revert Layer Keyword, Part Of Css Cascade Layers

Understanding the CSS revert-layer keyword

In this article, we’ll explore CSS cascade layers — and, specifically, the revert-layer keyword — to help you refine your styling strategy.

Chimezie Innocent
Apr 24, 2024 â‹… 6 min read
Exploring Nushell, A Rust Powered, Cross Platform Shell

Exploring Nushell, a Rust-powered, cross-platform shell

Nushell is a modern, performant, extensible shell built with Rust. Explore its pros, cons, and how to install and get started with it.

Oduah Chigozie
Apr 23, 2024 â‹… 6 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