Trying to write a post request to create a database with fiber and gorm and it gives me an error


trying to oupt a post (create) request using gorm as my database and fiber as my golang framewor but it seems like it is not working sometimes it returns "json: unmarshal(non-pointer, main.AcctDetails)" someone should please help me out i have been stuck here for quite a long time

package main

import (


var (
    DB  *gorm.DB
    err error

type AcctDetails struct {
        ID           uint    `json:"id"`
        AcctName     string  `json:"acctname"`
        AcctNumber   string  `json:"acctnumber"`
        UsersPhoneNo string  `json:"usersphoneno"`

func Routers(app *fiber.App) {
    app.Post("/bank", CreateUser)

func CreateUser(c *fiber.Ctx) error {
    info := new(AcctDetails)
    if err := c.BodyParser(info); err != nil {
        return c.Status(500).SendString(err.Error())

    return c.Status(200).JSON(&info)

func main() {
    app := fiber.New()
    db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
    if err != nil {
        panic("Cannot Connect To Database")

and it returns this as error

PS C:\Users\Chinonso\Desktop\New folder\GO\main> go run main.go

2022/04/29 22:47:51 C:/Users/Chinonso/Desktop/New folder/GO/main/main.go:45 SLOW SQL >= 200ms
[272.120ms] [rows:0] CREATE TABLE `acct_details` (`id` integer,`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`acct_name` text,`acct_number` text,`users_phone_no` text,PRIMARY KEY (`id`))

*this is after the server starts*

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

goroutine 8 [running]:*DB).Create(0xc0000c0000, {0x7ff7bc92e820, 0xc0000b4010})
        C:/Users/Chinonso/go/pkg/mod/ +0x22
        C:/Users/Chinonso/Desktop/New folder/GO/main/main.go:35 +0xe5*App).next(0xc0000023c0, 0xc0000c0000)
        C:/Users/Chinonso/go/pkg/mod/ +0x1be*App).handler(0xc0000023c0, 0x7ff7bc3a19b7)
        C:/Users/Chinonso/go/pkg/mod/ +0x45*Server).serveConn(0xc000127440, {0x7ff7bca94f30, 0xc000006ef8})
        C:/Users/Chinonso/go/pkg/mod/ +0x11ae*workerPool).workerFunc(0xc0002a9360, 0xc0002bd440)
        C:/Users/Chinonso/go/pkg/mod/ +0xa9*workerPool).getCh.func1()
        C:/Users/Chinonso/go/pkg/mod/ +0x38
created by*workerPool).getCh
        C:/Users/Chinonso/go/pkg/mod/ +0x1b5
exit status 2
PS C:\Users\Chinonso\Desktop\New folder\GO\main> 

please someone should help me resolve this code it’s giving me headach since two months now.


You are trying to access DB.Create() but didn’t assign db to global variable DB.
Note: using debugger could prevent a lot of headaches


    db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})


    DB, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{})

Answered By – medasx

Answer Checked By – David Goodson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.