DEV Community

Cover image for Build a REST API with Go - For Beginners

Build a REST API with Go - For Beginners

Karan Pratap Singh on September 01, 2021

In this article, we will build a simple REST CRUD API with Go. I've also created a video if you will like to follow along! Setup L...
Collapse
 
konjoinfinity profile image
Wesley Scholl

Thanks for putting this together, simple and straightforward. I was able to turn it into a simple full crud react native app!

github.com/konjoinfinity/rngo

Looking forward to part 2, thanks again.

Collapse
 
stravid87 profile image
John Stravid

I always appreciate concise, helpful tutorials that just work. Thank you!

Collapse
 
karimelngr profile image
Karim ElNaggar

Thanks for the tutorial, I think there is a small bug here

w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
Enter fullscreen mode Exit fullscreen mode

should be

w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Enter fullscreen mode Exit fullscreen mode

otherwise the content type header does not get written
Another thing, I think you switched two lines

router.HandleFunc("/books/{id}", handlers.GetBook).Methods(http.MethodGet)
// and
router.HandleFunc("/books", handlers.AddBook).Methods(http.MethodPost)
Enter fullscreen mode Exit fullscreen mode

between the two sections Add a new Book and Get a book by id

Thanks again for the great tutorial

Collapse
 
karanpratapsingh profile image
Karan Pratap Singh

Nice catch! Thanks, I have updated

Collapse
 
sethjoe12 profile image
karl joseph

hai sir good day can you help me with this thing .it was a code structure sir.

i edited all your codes and it comes with this error

"..\pkg\test\Additem.go:24:19: cannot use item (type models.Item) as type []byte in argument to json.Unmarshal"

Collapse
 
cerchie profile image
Lucia Cerchie

This was a great article! will there be a part 2 soon?

Collapse
 
karanpratapsingh profile image
Karan Pratap Singh

Hi Lucia, I'm glad this was helpful. yes there will be a part 2 where we will connect our API with gorm!

Collapse
 
karanpratapsingh profile image
Karan Pratap Singh
Thread Thread
 
cerchie profile image
Lucia Cerchie

this is awesome Karan! thanks!

Thread Thread
 
karanpratapsingh profile image
Karan Pratap Singh

Thanks!

Collapse
 
ramonivandy profile image
Ramon Ivandy Setiawan

Thank you for the tutorial!

But in Get All Books, there is a typo that confuse me a minute 😅 It should GetAllBooks.go instead GetBooks.go right?

Image description

Collapse
 
shaijut profile image
Shaiju T

I can see Go used by many Organization in cloud. But readability is not developer friendly like C# or Java. Have you tried Asp.net Core Web API to build API's its clean and fast ?