Learning Golang “Create simple API using Fiber”
By: Ari Nurcahya
Tags: Rest Api, Golang, Golang Tutorial, Fiber, API

Project structure

our app project structure will be like here

Because it is just a simple API in fiber so I didn’t make it as a complex folder structure. so what is that

Handlers

Handlers is a folder that will be a http handler, if you’re familiar with MVC pattern the handlers is just a controller in MVC pattern

localdata

because the app that we want to build still doesn’t use any database so we will declare static data. and all of the data will be placed at localdata. localdata has 2 file, data.go and user.model.go. data.go is a manipulation function, like a function for initialize data, and CRUD function. then user.model.go is a model

routes

routes is a folder for storing routes handler

Create Function that will serve all code

first we need to create server.go in our project directory, and the code will be like here

first we must create new instance from fiber, then we must pass it as a parameter in a routes function then we must listen the connection

Create Routes Function

here is a Routes function, in routes function we will define the parameter, and it is a pointer. why we must use pointer in the param? because when we define parameter as a pointer we can change data without returning it. because we just fill the variable memory with a new data, not change the value, but change the value of the memory address

Create Statics Datastorage

because our simple project didn’t use any database, so we must use statics data in our app. first we must create a struct for our data structure

then we mustcreate a new file called as data.go

then we must create a interface and struct

type Data interface {

GetData() []UserModel

GetDataById(id int) UserModel

DeleteData(id int) []UserModel

InsertData(UserModel) []UserModel

UpdateDataById(id int, user UserModel) []UserModel

} type data struct {

userList []UserModel

}

then we must create function for initialize the data

func InitData() Data {

return &data{

userList: []UserModel{

UserModel{

ID: 1,

Name: "A",

Age: 18,

Gender: "Male",

},

UserModel{

ID: 2,

Name: "Chi",

Age: 17,

Gender: "Female",

},

UserModel{

ID: 3,

Name: "Shintaro",

Age: 18,

Gender: "Male",

},

UserModel{

ID: 4,

Name: "Ayano",

Age: 18,

Gender: "Female",

},

},

}

}

then we must describe all of our abstract function in our interface.

first create GetData function

GetData function is a function that will returning all of userList data.

func (d *data) GetData() []UserModel {

return d.userList

}

GetDataById

this function will return one of data based on their id

func (d *data) GetDataById(id int) UserModel {

var foundIn int

var isFound bool

isFound = false

for idx, val := range d.userList {

if val.ID == id {

foundIn = idx

isFound = true

}

}

if isFound {

return d.userList[foundIn]

}

return UserModel{}

}

DeleteData

this function will return a list of data without data that had deleted before

func (d *data) DeleteData(id int) []UserModel {

var foundIn int

var isFound bool

isFound = false

for idx, val := range d.userList {

if val.ID == id {

foundIn = idx

isFound = true

}

}

if isFound {

d.userList[foundIn] = d.userList[len(d.userList)-1]

d.userList[len(d.userList)-1] = UserModel{}

d.userList = d.userList[:len(d.userList)-1]

return d.userList

}

return []UserModel{}

}

InsertData

This function will returning a List data with new data that had inserted before

func (d *data) InsertData(user UserModel) []UserModel {

user.ID = len(d.userList)

return append(d.userList, user)

}

UpdateData

This function will return a userList data with a updated data by their id

func (d *data) UpdateDataById(id int, user UserModel) []UserModel {

var foundIn int

var isFound bool

isFound = false

for idx, val := range d.userList {

if val.ID == id {

foundIn = idx isFound = true

}

}

if isFound {

d.userList[foundIn].Name = user.Name

d.userList[foundIn].Age = user.Age

d.userList[foundIn].Gender = user.Gender

}

return d.userList

}

And here is the full code