Create a Restful API with Golang from scratch

BackerLeader posted Originally published at sudoish.com 11 min read

In this tutorial, we are going to walk through an example of how to create a Restful API with Golang. We are going to cover the initial setup of the application, how to structure your app, how to handle persistence, docker config, HTTP requests and even debugging with vscode.
This tutorial covers many concepts since the basics of setting up an application, so I recommend that you check the table of contents below, decide how you want to follow this tutorial and maybe jump to the sections you are more interested in.

Why Golang?

Golang is an extremely lightweight and fast language that can be used to build CLIs, DevOps and SRE, web development, distributed services, database implementations, and much more. Applications like Docker and Kubernetes are written in Go for example.
Go is known for its performance and by its simplicity to learn (only 25 reserved keywords) and improve the existing codebase.

Initial setup

Create your folder and initialize the app

mkdir go-todo
cd go-todo
git init

Go has its own package manager, and it is handled through the go.mod file. To generate this base file you can run the following command:

go mod init <YOUR API NAME>

You can replace the <YOUR API NAME> for whatever name you want to give your app, but keep in mind that the convention is to use the name of the remote repository your app will be available.
For example github.com/pachecoio/go-todo

go mod init github.com/pachecoio/go-todo

This is especially important because that is how Go manages dependencies. Go does not download the dependencies to your local project directory, rather it handles them in a decentralized fashion through modules and we will be seeing that soon in the following sections.
If you want to dive deeper into this concept, you can check out the here to learn more about it.

Now let’s create our entry point file for this app.

touch main.go # Create a main.go file
code . # Open with vscode

Let’s include some boilerplate code to test our initial setup:

package main

import "fmt"

func main() {
    fmt.Println("App running")
}

You can run this app by running go main.go in your terminal or pressing F5 if you are using vscode.

Create your first endpoint with fiber

In this example, we will be using Fiber to handle our HTTP requests and create the base structure of the API.

Fiber is a Web Framework built with Fasthttp and inspired by Express, designed to ease things up for fast development with zero memory allocation and performance in mind.

First, let’s fetch the fiber dependencies:

go get github.com/gofiber/fiber/v2

Now we can import the fiber modules into the main.go file and replace the code inside the main function.

package main

import (
<spa

2 Comments

1 vote
1 vote

More Posts

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10

Starting From Scratch

MasterCraft - Feb 18

Merancang Backend Bisnis ISP: API Pelanggan, Paket Internet, Invoice, dan Tiket Support

Masbadar - Mar 13

How Go Slices Work Under the Hood: What Makes Them Stand Out from Other Languages

Ganesh Kumar - Mar 26

Introduction to Go generics?

Ganesh Kumar - Mar 30
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

31 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!