2 Getting Started
As a “baby gopher” I plan to:
- work in iterations (you can think of an iteration like a draft when you’re writing a paper).
- document each and every step as we build this package, and
- use the Go programming language to build this package.
2.1 Downloading Go
Visit the Go project’s downloads page and select the binary distribution that matches your operating system and processor architecture.
2.2 Learning Go
I learnt Go using the following resources:
- Main Golang Site
- A Tour of Go
- Go by Example
- How to Write Go Code
- Effective Go
- Go in Action book
- Error handling and Go
- JSON decoding in Go
- Interfaces: the awesome sauce of Go
- Go Code Review Comments
- Go: Best Practices for Production Environments
2.3 Install the Go tools
The Go binary distributions assume they will be installed in /usr/local/go (or c:\go under Windows), but it is possible to install them in a different location. If you do this, you will need to set the GOROOT environment variable to that directory when using the Go tools.
For example, if you installed Go to your home directory you should add the following commands to $HOME/.profile:
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
Under Windows, you may set environment variables through the “Environment Variables” button on the “Advanced” tab of the “System” control panel. Some versions of Windows provide this control panel through the “Advanced System Settings” option inside the “System” control panel.
The Go-environment works with a small number of OS environment variables. They are not required for building the Go-environment, but by setting them and thus overriding the defaults the Go compilation environment can be customized. In my Windows environment, I have set the following:
GOROOT `C:\go`
GOOS windows
GOARCH 386
In the same dialog-window: Edit the PATH variable as follows:
C:\go\bin; ...rest of PATH...
2.4 Editor for Go
Download and install and use any plain text editor of your choice to write your Go code.
2.5 Formatting code
Gofmt is a tool that automatically formats Go source code.
Instead of arguing about where curly braces should go, or whether to use tabs or spaces when you indent, the tool makes these decisions by applying a pre-determined style to Go source code.
Most seasoned Go developers configure their development environment to perform a go fmt on save or before committing to a code repository. In the misc directory in your Go installation there are plugins and instructions for how to do this for many common editors.
To format your code on the command line, you can use the the go fmt command:
$ go fmt path/to/your/package
2.6 Test your installation
Check that Go is installed correctly by building a simple program, as follows.
Create a file named hello.go in some folder (for now) and put the following program in it:
Program hello.go
package main
import "fmt"
func main() {
fmt.Println("Hello, world.")
}
Note: You can run the above program in the Go playgorund too.
Then from the folder where you have saved the file hello.go, run it with the go tool:
$ go run hello.go
hello, world
If you see the “hello, world” message then your Go installation is working.
2.7 Use GitHub
One of the best places to share your code with friends, co-workers, classmates, and complete strangers is GitHub.
2.7.1 Create an account
On the main screen that shows up, enter your username, your email and create a password. Next, click on Sign up for GitHub. On the next screen, The ‘Free’ plan is automatically chosen for you. Just click on the Finish sign up button. You will receive an email asking you to ‘Confirm your email’. Please do that.
2.8 Set up Git
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
If you are new to Git, then learn Git in 15 minutes.
Next, Set Up Git on your computer as per the guide.
2.9 Go Code Organization
We will create a simple Go package with the help of the go tool, the standard way to fetch, build, and install Go packages and commands.
The go tool requires you to organize your code in a specific way.
2.9.1 Workspaces
Go code must be kept inside a workspace. A workspace is a directory hierarchy with three directories at its root:
-
srccontains Go source files organized into packages (one package per directory), -
pkgcontains package objects, and -
bincontains executable commands.
The go tool builds source packages and installs the resulting binaries to the pkg and bin directories.
The src subdirectory typically contains version control repositories (such as for Git) that track the development of one or more source packages.
2.9.2 The GOPATH environment variable
The GOPATH environment variable specifies the location of your workspace.
To get started, create a workspace directory and set GOPATH accordingly. Your workspace can be located wherever you like. Note that this must not be the same path as your Go installation.
On my Windows computer, I have set GOPATH=C:\go_projects\go. Next I update my system environment variable PATH to include my workspace bin subdirectory i.e. PATH=%PATH%;%GOPATH%\bin;
My workspace folder structure
C:\go_projects
\---go
+---bin
+---pkg
\---src
2.9.3 Package paths
The packages from the standard library are given short paths such as fmt and net/http. For your own packages, you must choose a base path that is unlikely to collide with future additions to the standard library or other external libraries. You don’t need to worry about collisions. You can always alias the package name in the import.
If you have a GitHub account at github.com/user, that should be your base path. We will use github.com/user as our base path. Create a directory inside your workspace in which to keep source code. I have created the folder C:\go_projects\go\src\github.com\SatishTalim.
|
TipReplace |