...

/

Service Component Creation for REST API

Service Component Creation for REST API

Learn how to create a service component in a REST API with local storage.

Create the service component

A directory called services is created to store the service components, then inside that directory, the service component is created in the file called services.go. After that, some functionalities are added based on the project’s specifications. Inside services.go, a new variable called storage is created to store the item’s data.

package services

import (
	"go-simple-inventory/models"
)

var storage []models.Item = []models.Item{}

Get all items

Inside the services.go file, a function is created to get all items from the storage.

// GetAllItems returns all items data
func GetAllItems() []models.Item {
	return storage
}

Based on the code above, the GetAllItems() function returns the storage that contains all item ...