DEV Community

Cover image for Go Course: Hello world
Karan Pratap Singh
Karan Pratap Singh

Posted on • Originally published at karanpratapsingh.com

Go Course: Hello world

Let's write our first hello world program, we can start by initializing a module. For that, we can use the go mod command.

$ go mod init example
Enter fullscreen mode Exit fullscreen mode

But wait...what's a module? Don't worry we will discuss that soon! But for now, assume that the module is basically a collection of Go packages.

Moving ahead, let's now create a main.go file and write a program that simply prints hello world.

package main

import "fmt"

func main() {
    fmt.Println("Hello World!")
}
Enter fullscreen mode Exit fullscreen mode

If you're wondering, fmt is part of the Go standard library which is a set of core packages provided by the language.

Structure of a Go program

Now, let's quickly breakdown what we did here, or rather the structure of a Go program.

First, we defined a package such as main.

package main
Enter fullscreen mode Exit fullscreen mode

Then, we have some imports.

import "fmt"
Enter fullscreen mode Exit fullscreen mode

Last but not least, is our main function which acts as an entry point for our application, just like in other languages like C, Java, or C#.

func main() {
    ...
}
Enter fullscreen mode Exit fullscreen mode

Remember, the goal here is to keep a mental note, and later in the course, we'll learn about functions, imports, and other things in detail!

Finally, to run our code, we can simply use go run command.

$ go run main.go
Hello World!
Enter fullscreen mode Exit fullscreen mode

Congratulations, you just wrote your first Go program!


This article is part of my open source Go Course available on Github.

GitHub logo karanpratapsingh / learn-go

Master the fundamentals and advanced features of the Go programming language

Learn Go

Hey, welcome to the course, and thanks for learning Go. I hope this course provides a great learning experience.

This course is also available on my website and as an ebook on leanpub. Please leave a ⭐ as motivation if this was helpful!

Table of contents

What is Go?

Go (also known as Golang) is a programming language developed at Google in 2007 and open-sourced in 2009.

It focuses on simplicity, reliability, and efficiency. It was designed to combine the efficacy, speed…




Top comments (0)