Introduction to Go (golang) programming language.
Every thing you need to know as a beginner in Go.
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
The code above is a simple code that prints ‘Hello’ in English and Chinese. You can edit and play with it here called Go playground.
Go is an open source project developed by a team at Google and many contributors from the open source community. As the time of writing, it is on version 1.16 which was released on February, 2021. Go is statically typed, compiled programming language. It is syntactically similar to C, but with memory safety, garbage collection, structural typing and CSP style concurrency.
Go was designed in 2007 to improve programming productivity in an era of multicore, networked machines and large codebases. The designers wanted to address criticisms of other languauges in use at Google (Java, Python, C, C++), but keep their useful characteristics which includes:
Static typing and runtime efficiency in C.
Readability and usability in Python and Javascript.
high performance networking and processing.
The designers were primarily motivated by their shared dislike for C++.
What makes Go different?
The Go programming language was created with one goal in mind, to be able to build scalable web-applications for large scale audiences in a large team. So that is the reason they made the language as standardized as possible, hence the gofmt tool and the strict usage guidelines to the language was for the sake of not having two factions in the developer base, in other languages there are religious wars on where to keep the opening brace?
or for python should we use 4 spaces or 6 spaces or a tab or two tabs and other user preferences. If you know python then you might be aware of PEP8, which is a set of guidelines about how to write elegant code. While this might seem to be a shallow problem, when the codebase grows and more and more people work on the same code base it is becomes increasingly difficult to maintain the code's "beauty." We live in a world where robots can drive a car, so we shouldn't just write code, we should write elegant code. For other languages there are many variables when it comes to writing code. Every language is good for its use case, but Go is a little special because it was designed at a company which is the very synonym of the Internet (and distributed computing). Typically in order to optimize programs, developers choose to write Java over Python and C++ over Java, but almost all available languages widely in use were written decades ago when 1GB storage was much pricier. Now storage and computing is relatively cheap and computers are getting multiples cores, but the "old languages" are not harnessing concurrency in a way that go does. It's not because those languages are bad; utilizing concurrency wasn't a relevant usecase while the older languages evolved. To mitigate all the problems that Google faced with current tools, they wrote a systems language called Go which you are about to learn! There are many advantages to using golang and there are disadvantages too, for every coin has both sides. One of the significant improvements in in code formatting. Google has designed the language to avoid debates on code formatting. Go code written by anyone in the world (assuming they know and use gofmt ) will look exactly the same. This won't seem to matter until you work in a team! Also when the company uses automated code review or some other fancy technique, the formatted code may break in other languages which don't have strict and standard formatting rules, but not in go! Go was designed with concurrency in mind, please note that parallelism != concurrency, there is an amazing post by Rob Pike on the golang blog, you will find it there, it is worth a read.
To install Go into your machine, click here and look up the installation guide for your machine.
Hello World in Go.
To start writing Go code, You will need the following:
Some programming experience. The code here is pretty simple, but it helps to know something about functions.
A tool to edit your code. Any text editor you have will work fine. Most text editors have good support for Go. The most popular are VSCode (free), GoLand (paid), and Vim (free). I use VSCode.
A command terminal. Go works well using any terminal on Linux and Mac, and on PowerShell or cmd in Windows.
Open a command prompt and cd to your home directory.
On Linux or Mac:
cd
On Windows:
cd %HOMEPATH%
Create a hello directory for your first Go source code.
For example, use the following commands:
mkdir hello cd hello
Enable dependency tracking for your code.
When your code imports packages contained in other modules, you manage those dependencies through your code's own module. That module is defined by a go.mod file that tracks the modules that provide those packages. That go.mod file stays with your code, including in your source code repository.
To enable dependency tracking for your code by creating a go.mod file, run the
go mod init
command, giving it the name of the module your code will be in. The name is the module's module path. In most cases, this will be the repository location where your source code will be kept, such asgithub.com/mymodule
. If you plan to publish your module for others to use, the module path must be a location from which Go tools can download your module.
For the purposes of this tutorial, just use
example.com/hello
.
$ go mod init example.com/hello go: creating new go.mod: module example.com/hello
In your text editor, create a file hello.go in which to write your code.
Paste the following code into your hello.go file and save the file.
package main import "fmt" func main() { fmt.Println("Hello, World!") }
This is your Go code. In this code, you:
Declare a
main
package (a package is a way to group functions, and it's made up of all the files in the same directory).Import the popular
fmt
package, which contains functions for formatting text, including printing to the console. This package is one of the standard library packages you got when you installed Go.Implement a
main
function to print a message to the console. Amain
function executes by default when you run themain
package.
Run your code to see the greeting.
$ go run . Hello, World!
The
go run
command is one of manygo
commands you'll use to get things done with Go. Use the following command to get a list of the others:
$ go help
Call code in an external package
When you need your code to do something that might have been implemented by someone else, you can look for a package that has functions you can use in your code.
Make your printed message a little more interesting with a function from an external module.
Visit pkg.go.dev and search for a "quote" package.
Locate and click the
rsc.io/quote
package in search results (if you seersc.io/quote/v3
, ignore it for now).In the Documentation section, under Index, note the list of functions you can call from your code. You'll use the
Go
function.At the top of this page, note that package
quote
is included in thersc.io/quote
module.
You can use the pkg.go.dev site to find published modules whose packages have functions you can use in your own code. Packages are published in modules -- like
rsc.io/quote
-- where others can use them. Modules are improved with new versions over time, and you can upgrade your code to use the improved versions.
In your Go code, import the
rsc.io/quote
package and add a call to itsGo
function.
After adding the highlighted lines, your code should include the following:
package main import "fmt" import "rsc.io/quote" func main() { fmt.Println(quote.Go()) }
Add new module requirements and sums.
Go will add the
quote
module as a requirement, as well as a go.sum file for use in authenticating the module. For more, see Authenticating modules in the Go Modules Reference.
$ go mod tidy go: finding module for package rsc.io/quote go: found rsc.io/quote in rsc.io/quote v1.5.2
Run your code to see the message generated by the function you're calling.
$ go run . Don't communicate by sharing memory, share memory by communicating.
Notice that your code calls the
Go
function, printing a clever message about communication.
When you ran
go mod tidy
, it located and downloaded thersc.io/quote
module that contains the package you imported. By default, it downloaded the latest version -- v1.5.2.
Now you have installed Go and learned some of the basics.
Are you thinking about starting a career in computer programming? check this out
If you enjoyed this article and want to see other articles on web development and programming concepts, please share it to your friend and subscribe to my newsletter below. If you have any questions, leave your comments below.