I made a very simple first “API” in golang which I would like to share (most likely it does not have the best practices but at least it has the minimum requirements to scale into something more evolved).

Source Code

The first thing I wanted was to list the requirements of my API so that it is fully functional for what I needed.
- It should be able to connect through an ORM to the database since it will most likely change in the future (reference is azure sql at this time).
- Must be able to lift into a container and go through a continuous integration process to be like a public api
.- It must be scalable in time in terms of number of users and concurrent requests.
API design diagram

The first thing I investigated was the Golang documentation https://golang.org/ where the first thing is to follow the golang installation instructions very well on your machine either windows or mac https://golang.org/dl/

In my case I have a mac and what I did was to see if there was an installation through homebrew and I did it by following some very simple steps, taken from the following tutorial.

With “go version” you can see if the installation really works and what version of go you have

Another practical resource that helped me to build my first API was this:

However, concepts such as occupying an ORM and what ORM handles Golang are missing. From these “googling” I got to https://gorm.io/index.html which seemed like a very good option. Installation in your project

go get -u gorm.io/gorm

Now depending on the database to occupy, you should look for a dialect

You can see the different connections in

Another important dependency to install is Fiber
Fiber is an Express-inspired web framework built on top of Fasthttp, the fastest HTTP engine for Go. Designed to make things easy for rapid development with zero memory allocation and performance in mind.

installation

go get github.com/gofiber/fiber/v2

After that I will explain my simple API structure

Where the folder circleci, is the continuous integration and the steps to upload my api to a server.
You can watch this video to get an idea of what I do in that file

The result is something like this:

In .ssh are the private and public keys to upload the project, which is something like this.

Of course you must create your own
Terminal (also in the reference video it appears very well explained)
ssh-keygen -t rsa
In database the file database.go contains the connection package

and product is the model that will query the database with its respective verbs.

the file .env example you must replace it with .env where the environment variables are with yours.

The docker-compose.yml file can be replaced so that the orchestrator is kubernetes (locally it is more practical to orchestrate the API this way)

The dockerfile file is the minimum that the api requires to work and as you can see, they are very simple steps easy to replicate.

The main.go file contains the largest amount of application logic
Dependencies

Check for an .env file for environment variables.

Setting the environment variables

api routes

database initialization

principal function

Source Code

I hope this first approach helps you in your projects. (and in the future I will try to make a more detailed video). Any questions I will try to answer more often I even know that in old posts there are questions that I have not answered so I will try to update myself.

My job is to make your experience the best