Go: Get Started Pt. 1

Thursday, March 7, 2024 | Permalink

 Create a folder with name of the project e.g. project-name.

$ mkdir project-name

Go inside the created project-name folder.

$ cd project-name

Create a module or mod file using following command.

$ go mod init project-name

This command will create go.mod file. This file is the manifest file of the project that contains info of your project such as project name, Go version, dependencies of the project and so on. Following is the content as of now we’ve in this go.mod file.

module go-example 

go 1.21.5

We don’t have any dependency related information in this file yet. Because, our project yet not consuming any third-party package.

Create a file with name main.go and write following code in it.


package main


import "net/http"

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


func main() {

  r := gin.Default()

  r.GET("/", func (c *gin.Context) {

    c.JSON(http.StatusOK, gin.H{

      "message": "hello, world",

    })

  })

  r.Run()

}


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

$ go mod tidy

This command will look for the dependencies used in this project and download it for us. After successful completion of the command, if you look over the go.mod file again, it looks like this.

module project-name

go 1.21.5

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

require (
  github.com/bytedance/sonic v1.9.1 // indirect
  github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
  github.com/gabriel-vasile/mimetype v1.4.2 // 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.14.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.4 // indirect
  github.com/leodido/go-urn v1.2.4 // indirect
  github.com/mattn/go-isatty v0.0.19 // 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.0.8 // indirect
  github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
  github.com/ugorji/go/codec v1.2.11 // indirect
  golang.org/x/arch v0.3.0 // indirect
  golang.org/x/crypto v0.9.0 // indirect
  golang.org/x/net v0.10.0 // indirect
  golang.org/x/sys v0.8.0 // indirect
  golang.org/x/text v0.9.0 // indirect
  google.golang.org/protobuf v1.30.0 // indirect
  gopkg.in/yaml.v3 v3.0.1 // indirect
)

On line number 5, there we’ve the dependency. Remaining are the indirect dependency as they end with comment // indirect meaning that these dependencies are not directly consume by the project. But, project’s dependency are depends on it.

Finally, you should see a file with name go.sum. This file is auto generated by Go for the dependencies with checksum 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.

Labels: