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
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.
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.