Getting Started with Go: Your First ‘Hello, World!’ — Beginner Series 01
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:
- Visit the official Go website at https://golang.org/ to download the latest version of the Go compiler.
- 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.
Hello, World! in Go
To write your first Go code, set up the workspace using the commandgo 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.go
file package name must be "main."
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.
To run our program we can run the below command in the terminalgo run main.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.