Go or Golang is truly a next-gen programming language. It has been created by some of the best minds in the world of computing.

The great part about Go is that it’s good for beginners as well as experienced programmers.

Over the years, many tech companies (including Google) have adopted Go in a big way. A lot of the production workload in these companies is now running on Go. This makes a Go a very important language from a skill-set point of view as well.

You should absolutely try and learn Go programming language in 2023 (if you’ve not already done so).

In this post, I will explain a few important features of Go and also help you get started with your first Go program.

1 – The Features of Go

Let’s look at some of the core features of the Go programming language that make it a worthwhile language to learn.

  • Go is a compiled language. It’s not like JavaScript which is interpreted line-by-line. Neither is it like Java where your source code is converted to an intermediate format that requires a JVM environment to run.
  • Go also provides a lot of tools for doing routine tasks out-of-the-box.
  • With Go, you create executables for different operating systems such as MacOS, Windows and Linux.
  • Go is not an object-oriented language in the classic sense. In other words, you don’t have classes and things like overloading and overriding. Instead, you have structs in Go.
  • Go also misses some other usual things such as the try-and-catch block prevalent in most programming languages. According to the Go way of handling errors, try-catch blocks are not needed.
  • The Go Lexer does a lot of heavy lifting. For example, even though every statement in a Go program should end with a semicolon, it doesn’t throw a compilation error if you miss a semicolon. Instead, the Lexer determines where to have the semicolon and takes care of the same. Pretty cool, I’d say!
  • Lastly, Go has no limitations as to what you can create with it. The language is suitable for creating systems apps to web applications running on the Cloud.

2 – Installing Go

You can install Go on your development machine by visiting this link.

It contains detailed instructions for each operating system.

Once the installation is successful, you can check by executing the below command.

$ go version
go version go1.20.2 darwin/arm64

You can also learn more about the various go commands by executing this command.

$ go help
Go is a tool for managing Go source code.

Usage:

	go <command> [arguments]

The commands are:

	bug         start a bug report
	build       compile packages and dependencies
	clean       remove object files and cached files
	doc         show documentation for package or symbol
	env         print Go environment information
	fix         update packages to use new APIs
	fmt         gofmt (reformat) package sources
	generate    generate Go files by processing source
	get         add dependencies to current module and install them
	install     compile and install packages and dependencies
	list        list packages or modules
	mod         module maintenance
	work        workspace maintenance
	run         compile and run Go program
	test        test packages
	tool        run specified go tool
	version     print Go version
	vet         report likely mistakes in packages

To know about a particular command you can execute the help command for that specific item.

$ go help run

3 – Creating your First Go Program

Let’s now create our first Go program to get a basic feel of the language.

You can create a directory for the project and execute the below command.

$ go mod init hello

This will create a special file within your project directory with the name go.mod.

See the below sample:

module hello

go 1.20

If you have ever used Node.js, the go mod init command is not so different from the npm init command. It basically initializes a new project and creates a file to manage the project details.

Next, create another file with the name main.go. This is where we will write the source code of our super-simple Go program.

package main

import "fmt"

func main() {
     fmt.Println("Hello from ProgressiveCoder")
}

What’s going on over here?

  • The first line package main declares that this Go file belongs to the main package. In Go, a program must always have a main package. Think of it as the entry point for the executable. The main package is a special package because it’s the one that gets compiled into an executable program.
  • Next, we have the import "fmt" statement. This line basically imports the fmt package that stands for “format”. This package provides functions for formatting and printing text. It’s commonly used for input and output operations.
  • Moving on, we have the func main() statement. This defines the main function or the entry point of the program. In Go, the main function is where the program execution begins. It has no parameters and it’s function body is enclosed in curly braces.
  • Lastly, we use the fmt package to print a “Hello” message. In this line, we call the Println() function from the fmt package. This function prints a line of text to the standard output.

4 – Running the Go Program

To run this program, you can save the file and call the below command.

$ go run main.go
Hello from ProgressiveCoder

Note that when we run the go run command, there’s no binary saved in the project folder.

You might wonder if Go is actually compiling your program.

Rest assured that Go actually does compile your program. However, the binary is built in a temporary directory. After execution, Go deletes the binary. This makes go run pretty useful for testing out small programs.

Conclusion

That’s all for this post.

We covered the most important features that make Go such a cool programming language to learn in 2023.

Also, we looked at how to install Go on your system followed by creating and running your first Go program.

If you are looking to do more advanced stuff with Go, check out my post on creating an HTTP Web Server with Go.

If you have any comments or queries, please feel free to mention them in the comments section below.

Categories: BlogGolang

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *