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 / go-course

Master the fundamentals and advanced features of the Go programming language

Go Course

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 as well as on Educative.io

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, and safety of a statically typed and compiled language with the ease of programming of a dynamic language to…

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.