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
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!")
}
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
Then, we have some imports.
import "fmt"
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() {
...
}
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!
Congratulations, you just wrote your first Go program!
This article is part of my open source Go Course available on Github.
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
-
Getting Started
-
Chapter I
-
Chapter II
-
Chapter III
-
Chapter IV
-
Appendix
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)