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:

How To Create Heatmaps In Javascript: Exploring The Heat Js Library

How to create heatmaps in JavaScript: The Heat.js library

This tutorial will explore the application of heatmaps in JavaScript projects, focusing on how to use the Heat.js library to generate them.

Oghenetega Denedo
May 8, 2024 â‹… 7 min read
Eleventy Adoption Guide: Overview, Examples, And Alternatives

Eleventy adoption guide: Overview, examples, and alternatives

Eleventy (11ty) is a compelling solution for developers seeking a straightforward, performance-oriented approach to static site generation.

Nelson Michael
May 7, 2024 â‹… 8 min read
6 CSS Tools For More Efficient And Flexible CSS Handling

6 CSS tools for more efficient and flexible CSS handling

Explore some CSS tools that offer the perfect blend of efficiency and flexibility when handling CSS, such as styled-components and Emotion.

Fimber Elemuwa
May 7, 2024 â‹… 7 min read
Leveraging React Server Components In Redwoodjs

Leveraging React Server Components in RedwoodJS

RedwoodJS announced support for server-side rendering and RSCs in its Bighorn release. Explore this feature for when it’s production-ready.

Stephan Miller
May 6, 2024 â‹… 9 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