Getting Started with Go: Your First ‘Hello, World!’ — Beginner Series 01

Maneesha Indrachapa
5 min readFeb 21, 2024
Image from https://www.google.com/url?sa=i&url=https%3A%2F%2Fgolang-challenge.org%2Fwhy-golang-became-a-popular-language-for-data-science%2F&psig=AOvVaw2XkCik1JXtI7Dy5fTFx97M&ust=1708644773424000&source=images&cd=vfe&opi=89978449&ved=0CBMQjRxqFwoTCICl2ffLvYQDFQAAAAAdAAAAABAE

Go, often referred to as Golang, is an open-source programming language that emerged from the collaborative efforts of three esteemed Google engineers; Robert Griesemer, Rob Pike, and Ken Thompson. The language was officially announced in 2009 and has since experienced a surge in popularity among developers and organizations worldwide.

The design principles behind Go are rooted in simplicity, efficiency, and performance. The creators aimed to address common challenges faced by developers with existing languages, providing a streamlined and pragmatic approach to software development. Go is often characterized by its minimalist syntax and explicit design choices, making it easy to read, write, and maintain code.

Key Features in Go

we’ll check some key aspects that make Go a versatile and pragmatic language for developers.

  • Goroutines
    Go has native support for concurrent programming through Goroutines and channels. Goroutines are lightweight threads managed by the Go runtime, making it easy to write concurrent and parallel programs. Channels facilitate communication and synchronization between Goroutines.
  • Garbage Collection
    Go incorporates automatic garbage collection, relieving developers from manual memory management concerns. This feature enhances code reliability and reduces the risk of memory-related errors.
  • Static Typing
    Go is statically typed, meaning variable types are declared explicitly. This helps catch errors during compilation, ensuring code robustness. However, Go’s type system is designed to be expressive and flexible.
  • Fast Compilation
    Go is known for its quick compilation times, allowing for rapid development and iteration. Fast compilation is particularly beneficial for large codebases and projects with frequent updates.
  • Simplicity and Readability
    Go’s syntax is minimalistic and designed for readability. The language avoids unnecessary complexity and provides a clean and straightforward syntax, making it easier for developers to write and maintain code.
  • Standard Library
    Go comes with a comprehensive standard library that covers a wide range of functionalities, including networking, encryption, file I/O, and more. The standard library is well-designed and encourages consistency across Go projects.
  • Open Source and Community-driven
    Go is an open-source language with an active and supportive community. The open nature of Go encourages collaboration and the sharing of libraries and tools within the community.
  • Compatibility with C
    Go is designed to be compatible with C, allowing easy integration with existing C codebases. This makes it a suitable choice for systems programming and projects requiring interoperability with C libraries.

The points mentioned above highlight key features of Go. In addition to those, Go have several other positive attributes.

Installation and Setup

To begin coding in Go, follow these steps:

  1. Visit the official Go website at https://golang.org/ to download the latest version of the Go compiler.
  2. Install Go on your system by following the installation instructions provided on the website.

By completing these steps, you’ll have a fully set up Go development environment, ready for your coding endeavors. You can simply check it is working fine by typing the command go version in your terminal.

Fig 1.1-Check the go version in the terminal

Hello, World! in Go

To write your first Go code, set up the workspace using the command
go mod init <workspace-name>

Replace <workspace-name> with the desired name for your workspace. This command initializes a Go module, allowing you to manage dependencies for your project. After setting up the workspace, you're ready to start coding in Go. As an example, I created a new directory named ‘hello’ and created my workspace as ‘github.com/maneeshaindrachapa/hello-world`. Since we are putting our source code normally in GitHub it is good practice to create the workspace as github.com/<github-username>/<github-repository-name>

Next, we need to create a Go file to write our code, I already created a main.go file inside the ‘hello’ directory.

In Go, each file must declare a package name, and all files in a directory should have the same package name. Since we want to run our ‘hello world` programme we need the main function of go which is func main() function. The main function of Go needs to be inside package main so our main.gofile package name must be "main."

fig 1.2 — ‘Hello World’ program in Go

In our ‘Hello, World!’ program, we utilize the fmt package from the Go standard library. The fmt package is responsible for printing the message 'Hello, World!' in the terminal.

In Golang, the import statement is used to bring packages from the standard library or external dependencies into your code.

Fig 1.3 — `Importing` packages in Go

To run our program we can run the below command in the terminal
go run main.go

Fig 1.4 — Running the “Hello World” program in Go

That is how you run a “Go” code.

In wrapping up our first steps in the world of Go with this beginner series, we’ve covered key features of Go and from setting up our workspace to creating a ‘Hello, World!’ program, we’ve laid the foundation for understanding this powerful language. This is just the start. Go opens doors to vast possibilities.

--

--