Go is an open source, statically typed, compiled programming language built by Google.
It combines the best of both statically typed and dynamically typed languages and gives you the right mixture of efficiency and ease of programming. It is primarily suited for building fast, efficient, and reliable server side applications.
Following are some of the most noted features of Go -
- Safety : Both Type safety and Memory safety.
- Good support for Concurrency and communication.
- Efficient and latency-free Garbage Collection
- High speed compilation
- Excellent Tooling support
This is the first part of our tutorial series on Go. In this article, you’ll learn how to install Go in your system and set up your development environment for Go projects.
Installing Go
Go binary distributions are available for all major operating systems like Linux, Windows, and MacOS. It’s super simple to install Go from the binary distributions.
If a binary distribution is not available for your operating system, you can try installing Go from source.
Mac OS X
Using Homebrew
The easiest way to install Go in Mac OS is by using Homebrew -
brew install go
Using macOS package installer
Download the latest Go package (.pkg
) file from Go’s official downloads page. Open the package and follow the on-screen instructions to install Go. By default, Go will be installed in /usr/local/go
.
Linux
Download the Linux distribution from Go’s official download page and extract it into /usr/local
directory.
sudo tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz
Next, add the /usr/local/go/bin
directory to your PATH environment variable. You can do this by adding the following line to your ~/.bash_profile
file -
export PATH=$PATH:/usr/local/go/bin
You can also use any other directory like /opt/go
instead of /usr/local
for installing Go.
Windows
Download the Windows MSI installer file from Go’s official download page. Open the installer and follow the on-screen instructions to install Go in your windows system. By default, the installer installs Go in C:\Go
The Go tool
The Go distribution comes bundled with the go tool. It is a command line tool that lets you automate common tasks such as downloading and installing dependencies, building and testing your code, and much more.
After installing Go
by following the instructions in the previous section, you should be able to run the Go tool by typing go
in the command line -
$ go
Go is a tool for managing Go source code.
Usage:
go command [arguments]
The commands are:
build compile packages and dependencies
clean remove object files
doc show documentation for package or symbol
env print Go environment information
bug start a bug report
fix run go tool fix on packages
fmt run gofmt on package sources
generate generate Go files by processing source
get download and install packages and dependencies
install compile and install packages and dependencies
list list packages
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet run go tool vet on packages
Use "go help [command]" for more information about a command.
Additional help topics:
c calling between Go and C
buildmode description of build modes
filetype file types
gopath GOPATH environment variable
environment environment variables
importpath import path syntax
packages description of package lists
testflag description of testing flags
testfunc description of testing functions
Use "go help [topic]" for more information about that topic.
Go Code Organization
Go programs are organized into packages. A package is a collection of source files in the same directory that are compiled together. All the functions, types, variables, and constants defined in one source file are visible to all the other source files within the same package.
Go language has several built-in packages like
fmt
package, which contains functions for formatting and printing text.math
package, which provides basic constants and mathematical functions.
You need to import these packages in your program if you want to use the functions and constants defined in these packages. You can also import and use external packages built and published by other people on any source control management system like github.
Any Go source code repository contains one or more modules. A module is a collection of related Go packages stored in a directory with a go.mod
file at its root. The go.mod
file defines the module’s path, which is the import path used while importing packages that are part of this module.
When you import packages contained in other modules, you manage those dependencies through your code’s own module defined by the go.mod
file. The go.mod
file keeps track of all the external modules that provide the packages used by your code.
mymodule
app/
util/
go.mod
Hello World
program
Testing your Go installation with the Let’s write our first Go program to test our Go installation. Create a new folder hello
inside your home directory.
mkdir hello
cd hello
Now, let’s create a go module for our Hello world program. You can do so by running the go mod init
command with the module name. The module’s name is also its import path. In most cases, this will be the repository location where your source code will be kept, such as github.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.
Note that you don’t need to publish your code to a remote repository before you can build it. A module can be defined locally without belonging to a repository. However, it’s a good habit to organize your code as if you will publish it someday.
Let’s create the module like this;
go mod init github.com/callicoder/hello
go: creating new go.mod: module github.com/callicoder/hello
Now create a file named hello.go
and paste the following code:
package main
import "fmt"
func main() {
fmt.Printf("Hello, World\n")
}
You can run the Hello world progeam by running go run .
command
$ go run .
Hello, World
go build
Building an executable binary using The go run
command compiles and runs your program at one go. If however, you want to produce a binary from your Go source that can be run as a standalone executable without using the Go tool, then use the go build
command -
$ go build
$ ls
go.mod hello hello.go
The go build
command creates an executable binary with the same name as the name of your immediate package (hello
). You can run the binary file like so -
$ ./hello
Hello, World
bin
directory using go install
Installing the package into the You can use the go install
command to build and install the executable binary into your Go workspace’s bin
directory ($HOME/go/bin
) -
$ go install
$ cd $HOME/go/bin
$ ls
hello
$ ./hello
Hello, World
You can also add the $HOME/go/bin
directory to the PATH
variable to run go executables from any location.
Don’t forget to check out: go help run
, go help build
, go help install
.
Conclusion
That’s all for now folks! I hope you’re all set to deep dive and learn more about the Go programming language. You can find all the tutorials written in Go from the Golang categories page.
Thanks for reading, see you in the next article.
Next Article: Hello Golang: Writing your first Golang Program
Code Samples: github.com/callicoder/golang-tutorials