Go is an opensource programming language to build simple, reliable, and performant distributed systems.
Fiber is a web framework written in Go built on top of FastHTTP that is faster than the Golang HTTP standard library. It has a very similar design approach to ExpressJS.
To start you will need to have Golang installed on your machine. To verify, go to your terminal and type.
go version
You should get back something similar to this.
go version go1.15.2 linux/amd64
Since now we have everything in place, let’s get started.
Let’s start by creating our project folder. First, go to a location where you want to keep your project.
cd ~/go/github.com/bootmind
Then, on your terminal type the following to create it.
mkdir figo
Now you should enter it.
cd figo
I’ve created this project name, because we are going to create a financial application.
We want to register our daily expenses having the following information:
- Title (text)
- Total (number)
- Created at (date)
- Receipt (file)
With these four fields, we will learn how to validate strings, numbers, dates, and files, store data in a database, and upload a file using Golang.
And a very interesting thing, we will store the data in the database and save the file in disk using concurrency. Nice isn’t it?
We will use the Golang dependency management system, the Go Modules. To do so, we need to initialize our project with go mod.
I’m going to use Bootmind’s Github URL to that. In your case, you should use yours.
go mod init github.com/bootmind/figo
You will see that now you have two files, go.mod and go.sum.
To create our Gofiber project, we need to install it.
go get github.com/gofiber/fiber/v2
Open the project on your favorite text editor, in my case is VSCode.
On my terminal I have the command to open it.
code .
Inside of it, create your main.go file that should contain.
package main
import "github.com/gofiber/fiber/v2"
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.JSON(map[string]string{
"message": "Figo API",
})
})
app.Listen(":3000")
}
On line 6, we are creating a new Fiber instance.
On line 8, we are using the Get function from Fiber to receive the HTTP Get requests. This function expects a path as the first argument and a handler function with a pointer to the Fiber context.
On line 14, we are telling Fiber to listen to the HTTP requests on port 3000.
Now run on your project root path. Just to remind you, mine is ~/go/github.com/bootmind/figo
go run main.go
Go to your browser on http://localhost:3000. You should see the Figo API message in a JSON format on your screen.
Github repository: https://github.com/bootmind/figo-api
Part 02 https://blog.bootmind.com/golang/how-to-create-an-api-with-golang-and-fiber-part-02/
Part 03 https://blog.bootmind.com/golang/how-to-create-an-api-with-golang-and-fiber-part-03/
Part 04 https://blog.bootmind.com/golang/how-to-create-an-api-with-golang-and-fiber-part-04/
Part 05 https://blog.bootmind.com/golang/how-to-create-an-api-with-golang-and-fiber-part-05/

Leave a Reply