DEV Community

Leandro Lima
Leandro Lima

Posted on

Getting Started with Golang: A Beginner's Guide to Writing Go Code

Getting Started with Golang: A Beginner's Guide to Writing Go Code

Are you looking to jumpstart your journey into the world of programming and learn how to write Go code? If so, you've come to the right place! In this beginner's guide, we'll cover the fundamentals of Go programming, from understanding the language, installing dependencies and writing simple "Hello World" programs.

Go, often referred to as “Golang,” is a programming language developed by Google in 2009. It was created to address some of the shortcomings of existing languages, and has since gained popularity in many areas, including web development, system operations and database programming.

Setting Up Your Environment

Before you can start writing and running Go code, you first need to set up your environment and install the necessary language dependencies. Go runs on Linux, Mac OS and Windows systems. To get started, head to the Go downloads page and select the version that corresponds to your operating system.

Once the download is complete, you are now ready to install Go.

Installing Go on Windows and Mac

To install Go on a Windows or Mac system, double click the downloaded file and follow the on-screen instructions. Once the installation is complete, open the terminal window, and run the following command to set up the environment:

go env
Enter fullscreen mode Exit fullscreen mode

Installing Go on Linux

If you are using a Linux environment, you install Go by using the package manager apt and the following command:

sudo apt-get install golang-1.7
Enter fullscreen mode Exit fullscreen mode

Make sure that the version of Go in the command matches the one you've downloaded, as version numbers can change with time. Once installed, you can also set up the environment by running the go env command as described above.

Writing and Running Your First Go Program

Now that you have your environment set up, let's create and run your first Go program. Open the code editor, and enter the following code into a blank file. This simple program will output "Hello World!" to the terminal:

// Hello World program in Go

package main

import "fmt"

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

To run the program, open up your terminal window, navigate to the directory where you saved your file, and run the following command:

go run hello_world.go
Enter fullscreen mode Exit fullscreen mode

If all goes well, you should see the words "Hello World!" printed to the screen. Congratulations -- you've just written and run your very first Go program!

Anatomy of a Go Program

At a high level, there are three core components to a Go program -- package declarations, import statements, and func or function definitions.

Package Declarations

When writing a Go program, the first line must always contain a package statement. This statement informs Go which package the program belongs to. For example, the statement package main, as seen in the "Hello World" program above, lets Go know that the program will be compiled into an executable program.

Import Statements

An import statement tells Go which dependencies are needed to run the program. For example, in the "Hello World" program, the fmt package is imported. This package includes library functions for formatting output strings.

Function Definitions

Functions are pieces of code that can be used to perform specific tasks. In Go, the most commonly used type of function is a main function. This is the entry point for programs, and is the first function called when the program is executed.

Within the main function, you can call library functions and other code blocks to perform tasks. In the example above, the fmt.Println() method is used to output the string "Hello World!" to the screen.

Learn More

Go is an expansive language and there is much more to learn beyond this guide. For more resources and tutorials, you can check out these two blog posts:

Top comments (0)