Go: Get Started Pt. 1

Mar 7, 2023

Create a new folder with iconic as the folder name.

mkdir iconic

Go inside the created iconic folder.

cd iconic

Create a module or mod file using the following command.

go mod init iconic

This command creates the go.mod file. This is the project manifest file contains the info of the project, Go version, dependencies of the project, and so on.

Open this go.mod file and you should see following content.

module iconic

go 1.21.5

---

Let’s create a main.go file and write the following code in it.

touch main.go
package main

import (
  "net/http"
  "github.com/gin-gonic/gin"
)

func main() {
  r := gin.Default()

  r.GET("/ping", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{
      "hello": "world",
    })
  })

  r.Run()
}

This is the simple hello, world program in Gin framework. Notice that we are importing Gin framework from http://github.com/gin-gonic/gin. But, we haven’t installed this package yet from this location. In order to install the package, we need to run following command.

go mod tidy

This command looks for the dependencies used in this project and download it for us. After successful completion of the command, if you check the content of the file go.mod file, then it is now updated and looks like this.

module iconic

go 1.21.5

require github.com/gin-gonic/gin v1.10.0

require (
  github.com/bytedance/sonic v1.11.6 // indirect
  github.com/bytedance/sonic/loader v0.1.1 // indirect
  github.com/cloudwego/base64x v0.1.4 // indirect
  github.com/cloudwego/iasm v0.2.0 // indirect
  github.com/gabriel-vasile/mimetype v1.4.3 // indirect
  github.com/gin-contrib/sse v0.1.0 // indirect
  github.com/go-playground/locales v0.14.1 // indirect
  github.com/go-playground/universal-translator v0.18.1 // indirect
  github.com/go-playground/validator/v10 v10.20.0 // indirect
  github.com/goccy/go-json v0.10.2 // indirect
  github.com/json-iterator/go v1.1.12 // indirect
  github.com/klauspost/cpuid/v2 v2.2.7 // indirect
  github.com/leodido/go-urn v1.4.0 // indirect
  github.com/mattn/go-isatty v0.0.20 // indirect
  github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
  github.com/modern-go/reflect2 v1.0.2 // indirect
  github.com/pelletier/go-toml/v2 v2.2.2 // indirect
  github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
  github.com/ugorji/go/codec v1.2.12 // indirect
  golang.org/x/arch v0.8.0 // indirect
  golang.org/x/crypto v0.23.0 // indirect
  golang.org/x/net v0.25.0 // indirect
  golang.org/x/sys v0.20.0 // indirect
  golang.org/x/text v0.15.0 // indirect
  google.golang.org/protobuf v1.34.1 // indirect
  gopkg.in/yaml.v3 v3.0.1 // indirect
)

As mentioned on line number 5, the project requires to have the Gin framework. But, this Gin framework depends on other modules or packages which are listed below with comment // indirect at the end.

Finally, you should see a new file with name go.sum. This file is auto-generated by Go for the dependencies checkup to figure out whether to download the dependencies again or not. Let’s not worry about this file as it’ll be created, updated, and managed by Go for us.

---

In previous example, we first used the module and then downloaded it. You can do reverse as well. To download the package, following command is used.

go get -u github.com/gin-gonic/gin

The above command install or update (-u) the package Gin. And then you can use it within project.

Tags: go