DEV Community

Cover image for Go Fiber: Start Building RESTful APIs on Golang (Feat. GORM)

Go Fiber: Start Building RESTful APIs on Golang (Feat. GORM)

Gaurav Mehra on September 14, 2021

πŸ“š Contents What are we building Go Express and Fiber Let's Get Started With APIs Let's create an endpoint Go modules Setup E...
Collapse
 
bensonthedean profile image
softwarepanda • Edited

First of all thank you for this amazing tutorial. I am a newbie in Golang language and i must say i am loving the experience so far. However on completion of this tutorial i am getting this error. If anyone could help me decode, i will appreciate.

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x28 pc=0xe56458]

goroutine 7 [running]:
gorm.io/gorm.(*DB).getInstance(0x167e7f70598)
C:/Users/BENSON OPISA/go/pkg/mod/gorm.io/gorm@v1.23.1/gorm.go:363 +0x18
gorm.io/gorm.(*DB).Find(0x167ed27dfe0, {0x12a7180, 0xc00009f4a0}, {0x0, 0x0, 0x0})
C:/Users/BENSON OPISA/go/pkg/mod/gorm.io/gorm@v1.23.1/finisher_api.go:159 +0x45
github.com/bensonopisa/notes-app/internal/handlers/note.GetNotes(0xc000428000)
D:/projects/golang/notes-app/internal/handlers/note/note.go:20 +0x69
github.com/gofiber/fiber/v2.(*App).next(0xc0000023c0, 0xc000428000)
C:/Users/BENSON OPISA/go/pkg/mod/github.com/gofiber/fiber/v2@v2.27.0/router.go:132 +0x1d8

Collapse
 
bensonthedean profile image
softwarepanda

This is thrown on the line db.Find(&notes) on the getNotes Function on notehandler package.

Collapse
 
percoguru profile image
Gaurav Mehra

Sorry for the late reply. Seems like you are trying to access an uninitiated variable or nil value. Can you check if you are doing something different?

Collapse
 
guhkun13 profile image
name cannot be blank

Great and many thanks for the detail explanation, its really help.
Just curious about folder structure, why not naming it "handlers/note.go" instead of handlers/noteHandlers.go or routes/noteRoutes.go.

Collapse
 
percoguru profile image
Gaurav Mehra

Thanks man! Glad you liked it.
If you mean that why we did not use handlers/noteHandlers.go. The reason for this would be when we add more handlers, it would become handlers/noteHandlers.go and handlers/userHandlers.go. It would be multiple packages inside the same folder which go does not like a lot.
I hope I understood your question.

Collapse
 
rngallen profile image
Luqman Jr

One of the best Fiber tutorial, I would like to know is it possible to use basic auth with gotemplates engine? I can't find inputs for this scenario.

Collapse
 
arunap2509 profile image
arun_ap

Hi,
I am getting unexpected fault address 0xed9497c2d
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x1 addr=0xed9497c2d pc=0x4f8f6a]
when trying to return from getnotes
return ctx.JSON(fiber.Map{"status": http.StatusOK, "message": "notes found", "data": notes}), when returing null its working, and also when returning a hardcoded struct its working, but its breaking when returning the data returned from db, any idea why..

Collapse
 
arunap2509 profile image
arun_ap • Edited

Never mind, seems like an issue in the latest version of fiber, can be fixed by
app := fiber.New(fiber.Config{
JSONEncoder: json.Marshal,
JSONDecoder: json.Unmarshal,
})

Collapse
 
asrie profile image
Asrie Zulfikar Firdaus

nice info, good tutorial..

Collapse
 
go-lang-developer profile image
sunv

First I saw basic tutorial at youtube.com/playlist?list=PLs1BaES... and then i implemented code using this blog. @percoguru you made my day. Thank you So much.. keep writing

Collapse
 
skylersaville profile image
Skyler Saville

Wow! This is such a well written tutorial! I’ve been writing Express CRUD apps for years, but I’ve just started my Go journey. So happy to have stumbled across this gem! Keep up the great work!

Collapse
 
percoguru profile image
Gaurav Mehra

Thanks a lot!!

Collapse
 
cybernamix profile image
Cybernamix

Thank you! Nice tutorial

Collapse
 
arthemis97 profile image
Tod-Od

Thank you :)