learning-go

Learn Go by building a REST API and a Command Line Interface (CLI)

Start the API project

In this step we will:

  1. Create the API folder
  2. Init the git repository
  3. Enable dependency tracking for the Go code
  4. Create the first Go file
  5. Create a repository in github.com
  6. Commit and push the initial version

The following commands are needed for steps 1, 2, and 3:

mkdir learning-go-api && cd learning-go-api
git init .
go mod init github.com/renato0307/learning-go-api

Replace the renato0307 part by the correct value for you. This should be the value of a GitHub repository where you will store the project.

Next, open vscode by typing:

code .

Create a file named main.go with the following contents:

package main

import "github.com/gin-gonic/gin"

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "Hello, welcome to the learning-go-api",
		})
	})
	r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

We are going to use Gin Tonic as the HTTP web framework to develop our API.

To run this code, go back to the command line and enter:

go run main.go

Visit "localhost:8080" on browser or use httpie.

http localhost:8080

You should see a result similar to:

HTTP/1.1 200 OK
Content-Length: 51
Content-Type: application/json; charset=utf-8
Date: Tue, 21 Dec 2021 06:52:21 GMT

{
    "message": "Hello, welcome to the learning-go-api"
}

Next, go to https://github.com and create a repository.

In my case I named it learning-go-api.

After the repository is created, add it as remote origin and do the initial commit and push:

echo "# learning-go-api" >> README.md
git add .
git remote add origin git@github.com:renato0307/learning-go-api.git
git commit -m "initial version"
git branch -M main
git push -u origin main

With this all set, we are ready to start doing some real code.

Next

The next section is Add programming/uuid generator function to the API.