Go for Golang - Introduction Environment Setup
Get your feet wet with Goland and this introductory tutorial that sets it up.
Join the DZone community and get the full member experience.
Join For FreeI have been working with Go (also known as Golang) for some time now, and I must tell you that the more I work with it, the more I like it. And that’s the inspiration to set me on this path of sharing my learning about Go. That said, here is what we will be talking about in this article:
- Introduction to Go
- Environment setup
- Hello, World! – following the tradition
Let’s get started!
Introduction to Go (Golang)
Go is a general-purpose language, which was designed with systems programming in mind. At first, Go feels more or less like C or C++, but it’s certainly more than that. Three key aspects that make Go stand out are that it is compiled, garbage-collected, and above all, it has explicit support for concurrent programming.
Compiled: Go is compiled. When we say so, it’s important to understand that it is compiled down to the processor level. This means that when we compile our code, it is compiled into a code that runs directly on the processor.
Garbage Collected: In general, garbage collection means that the memory is managed for you. Go free up a developer from worrying about allocating or freeing memory manually. In Go, garbage collection is very fast as compared to other programming languages that have built in garbage collection, where it can create some latency in the application at an unpredictable time.
Concurrent Programming: Go is concurrent, and this concurrency is built right into the language. The Go routines allow us to handle concurrency by communicating through messages or passing the data instead of sharing it. This gives Go, the capability of taking advantage of modern processor where we have multiple cores and multiple processors almost everywhere.
Environment Setup
To set up the environment and getting started with Go, the first thing we need is to install Go and there is no better place (with description) than Go’s Downloads page. Once we select the package based on our OS, we land on a page that describes the steps to setup Go in the PATH
environment variable, followed by setting the GOPATH
.
Go Installation Directory
When we install Go, it creates a directory c:\go
on Windows and /usr/local/go
in UNIX. This is where all the necessary binaries are kept by Go to make command line tools, compiler and standard library to work. So, when we import a package from Go’s standard library, Go looks for that package in this directory.
If we want to change the installation directory of Go, we can very well do so. However, to let Go know about the change, we are required to update the value of GOROOT
variable which is by default set to default installation directory. On Windows, we can do so by updating the GOROOT
environment variable, and on UNIX we can edit the ~/.bash_profile
and set the GOROOT
as: export GOROOT=/usr/local/custom-go
.
I would suggest not changing the Go root, until it’s really required and is your last resort.
Go Workspace
A workspace is a way to facilitate project management in Go. A workspace is simply a directory on our system, where Go looks for source code files, manages dependency packages and build distribution binary files. We can have as many workspaces as we want, however, we must ensure that the GOPATH
environment variable must point to current working workspace directory.
To learn more about Go workspace and its directory structure, do check out this documentation page. Any Go workspace directory must have three subdirectories viz. src
, pkg
and bin
.
Go Workspace Example
src
is project directory containing the Go source code (.go
files). Also, any packages installed using go get
command or any dependency packages reside in this directory. In Go, all the source files are contained in a package. Therefore, when we work on a project, we create a directory inside the $GOPATH/src
directory, and keep our source files in it.
pkg
is a directory that contains the Go package objects. When we build our Go source code, they are archived here and therefore have the .a
file extension. These files are created by the Go pack tool when we build a package. These files contain source information, debug symbols, and compiled package binary code. Package objects are compiled and archived files for specific platforms, and therefore we can see a platform architecture directory inside pkg directory (refer the above image).
bin
directory contains the binary executable files, which can be created using the go install
command. The go install
command internally first builds the package using the go build
command and then outputs the binary executable files in the bin
directory. Go uses the GOBIN
environment variable to place the executables, which is by default set to $GOPATH/bin
and can be changed.
Once everything is set, we can run the go env
command to see the environment variables set for Go. Here is what it looks like on my Mac:
go env
Note the values for GOROOT
and GOPATH
, and if everything looks fine we are all set to say “Hello, World!“.
Hello, World!" in Go(Golang)
Let’s now continue following the long going tradition of writing a program which prints Hello, World!
. To start with, please ensure that you have setup the environment, and the output of go env
command shows what you expect. Here is what we need to do:
- create a directory inside
$GOPATH/src
as hello-world, or name it whatever you like - inside this directory, create a file as
hello.go
- open the file in your favourite editor and copy-paste the code given below
- save the file
- being inside the directory created above, run the
go run hello.go
command - it will first build our code, run it for us and print the output on console
Hello, World! in Go:
package main // importing main package declares that this Go package is an executable
import "fmt" // import the fmt (short for format) package
// main is the entry point for an executable
// main function, in Go, does not take any arguments
func main() {
// print the message to standard output
fmt.Println("Hello, World!\n")
}
The image below shows the above steps in action:
Hello, World! in Go
If you can see, “Hello, World!” printed on your console, congratulations! You have completed your first milestone. We will not dive into the program structure right now, but the inline comments in the code should give an idea about what each line does.
Summary
Go, also know as Golang, is a general-purpose programming language which is based on C and C++, but is not limited. What makes go stand out the most is it’s built-in support for concurrency and garbage collection. In this article, however, we covered a brief introduction to Go, its working environment and how we can setup Go to start coding. There is certainly much more to come. Go is easy to learn. To make the best out of Go, don’t code your way in Go, code the way Go suggests and solve your problems.
Published at DZone with permission of Gaurav Gahlot, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments