Skip to main content

Command Palette

Search for a command to run...

Hello World in Go

Updated
2 min read

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.

Go Programming Language - Basic To Advance

Part 4 of 5

This series is all about Go Programming Language. We will start from basic features and then discuss about creating large projects with best practices and at the end also cover system design and design patterns using Golang.

Up next

Why Go!

First of all, why Go? Go was created in 2007 at Google for following reasons Slow Compilation: C++ and Java code at Google was too slow to compile. They needed something that could compile quickly Complexity: Existing languages had grown to become ...

More from this blog

P

Programming, System Design and AI | Latencot

15 posts

Hear, you will read all about tech and tech updates. From writing code to building AI systems, we'll talk and share about everything.