Hello World in Go

Go can be downloaded through the official website; click here.
Once you have installed Go, to check if go has been correctly installed, you can open a terminal and write below comand.
go version
This should show you current version of Go.
Next. Create a folder for your project and inside that folder, open terminal and type
go mod init <project-name>
this will create a go.mod file which will have a list of all your dependencies for the project. You can think of this as the package.json in Javascript / Typescript projects.
Create a new file with name main.go
, inside that file, write the below code
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Save the file, and run below command
go run main.go
OR
go run .
This should run your project and print “Hello, World!“
Let’s talk about each component of this file
The first line package main
, tells that this file belongs to package main. Consider package as a grouping method to group multiple types, entities under one name.
The second line is import fmt
which is basically an import statement. We are importing the fmt package in our file. This fmt packages allows us to do many things, one of them is printing on the console.
In next set of lines, we have declared a function main
, which acts as an entry point for the application. If you change the function name with something else, you won’t be able to run the application.
Functions in Golang are declared using func
. In the function we are calling the Println function from the fmt package that we imported above the main function. We are passing “Hello, World!“ to the print function. This string is then printed on the console.
Subscribe to my newsletter
Read articles from Sankalp pol directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
