Wednesday, January 23, 2019

How to Write Go Code

Introduction

This document demonstrates the development of a simple Go package and introduces 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. Please read this document carefully. It explains the simplest way to get up and running with your Go installation.
A similar explanation is available as a screencast.

Code organization

Overview

  • Go programmers typically keep all their Go code in a single workspace.
  • A workspace contains many version control repositories (managed by Git, for example).
  • Each repository contains one or more packages.
  • Each package consists of one or more Go source files in a single directory.
  • The path to a package's directory determines its import path.
Note that this differs from other programming environments in which every project has a separate workspace and workspaces are closely tied to version control repositories.

Workspaces

A workspace is a directory hierarchy with two directories at its root:
  • src contains Go source files, and
  • bin contains executable commands.
The go tool builds and installs binaries to the bin directory.
The src subdirectory typically contains multiple version control repositories (such as for Git or Mercurial) that track the development of one or more source packages.
To give you an idea of how a workspace looks in practice, here's an example:
bin/
    hello                          # command executable
    outyet                         # command executable
src/
    github.com/golang/example/
        .git/                      # Git repository metadata
 hello/
     hello.go               # command source
 outyet/
     main.go                # command source
     main_test.go           # test source
 stringutil/
     reverse.go             # package source
     reverse_test.go        # test source
    golang.org/x/image/
        .git/                      # Git repository metadata
 bmp/
     reader.go              # package source
     writer.go              # package source
    ... (many more repositories and packages omitted) ...
The tree above shows a workspace containing two repositories (example and image). The example repository contains two commands (hello and outyet) and one library (stringutil). The image repository contains the bmp package and several others.
A typical workspace contains many source repositories containing many packages and commands. Most Go programmers keep all their Go source code and dependencies in a single workspace.
Note that symbolic links should not be used to link files or directories into your workspace.
Commands and libraries are built from different kinds of source packages. We will discuss the distinction later.

The GOPATH environment variable

The GOPATH environment variable specifies the location of your workspace. It defaults to a directory named goinside your home directory, so $HOME/go on Unix, $home/go on Plan 9, and %USERPROFILE%\go (usually C:\Users\YourName\go) on Windows.
If you would like to work in a different location, you will need to set GOPATH to the path to that directory. (Another common setup is to set GOPATH=$HOME.) Note that GOPATH must not be the same path as your Go installation.
The command go env GOPATH prints the effective current GOPATH; it prints the default location if the environment variable is unset.
For convenience, add the workspace's bin subdirectory to your PATH:
$ export PATH=$PATH:$(go env GOPATH)/bin
The scripts in the rest of this document use $GOPATH instead of $(go env GOPATH) for brevity. To make the scripts run as written if you have not set GOPATH, you can substitute $HOME/go in those commands or else run:
$ export GOPATH=$(go env GOPATH)
To learn more about the GOPATH environment variable, see 'go help gopath'.
To use a custom workspace location, set the GOPATH environment variable.

Import paths

An import path is a string that uniquely identifies a package. A package's import path corresponds to its location inside a workspace or in a remote repository (explained below).
The packages from the standard library are given short import 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.
If you keep your code in a source repository somewhere, then you should use the root of that source repository as your base path. For instance, if you have a GitHub account at github.com/user, that should be your base path.
Note that you don't need to publish your code to a remote repository before you can build it. It's just a good habit to organize your code as if you will publish it someday. In practice you can choose any arbitrary path name, as long as it is unique to the standard library and greater Go ecosystem.
We'll use github.com/user as our base path. Create a directory inside your workspace in which to keep source code:
$ mkdir -p $GOPATH/src/github.com/user

Your first program

To compile and run a simple program, first choose a package path (we'll use github.com/user/hello) and create a corresponding package directory inside your workspace:
$ mkdir $GOPATH/src/github.com/user/hello
Next, create a file named hello.go inside that directory, containing the following Go code.
package main

import "fmt"

func main() {
 fmt.Println("Hello, world.")
}
Now you can build and install that program with the go tool:
$ go install github.com/user/hello
Note that you can run this command from anywhere on your system. The go tool finds the source code by looking for the github.com/user/hello package inside the workspace specified by GOPATH.
You can also omit the package path if you run go install from the package directory:
$ cd $GOPATH/src/github.com/user/hello
$ go install
This command builds the hello command, producing an executable binary. It then installs that binary to the workspace's bin directory as hello (or, under Windows, hello.exe). In our example, that will be $GOPATH/bin/hello, which is $HOME/go/bin/hello.
The go tool will only print output when an error occurs, so if these commands produce no output they have executed successfully.
You can now run the program by typing its full path at the command line:
$ $GOPATH/bin/hello
Hello, world.
Or, as you have added $GOPATH/bin to your PATH, just type the binary name:
$ hello
Hello, world.
If you're using a source control system, now would be a good time to initialize a repository, add the files, and commit your first change. Again, this step is optional: you do not need to use source control to write Go code.
$ cd $GOPATH/src/github.com/user/hello
$ git init
Initialized empty Git repository in /home/user/work/src/github.com/user/hello/.git/
$ git add hello.go
$ git commit -m "initial commit"
[master (root-commit) 0b4507d] initial commit
 1 file changed, 1 insertion(+)
  create mode 100644 hello.go
Pushing the code to a remote repository is left as an exercise for the reader.

Your first library

Let's write a library and use it from the hello program.
Again, the first step is to choose a package path (we'll use github.com/user/stringutil) and create the package directory:
$ mkdir $GOPATH/src/github.com/user/stringutil
Next, create a file named reverse.go in that directory with the following contents.
// Package stringutil contains utility functions for working with strings.
package stringutil

// Reverse returns its argument string reversed rune-wise left to right.
func Reverse(s string) string {
 r := []rune(s)
 for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
  r[i], r[j] = r[j], r[i]
 }
 return string(r)
}
Now, test that the package compiles with go build:
$ go build github.com/user/stringutil
Or, if you are working in the package's source directory, just:
$ go build
This won't produce an output file. Instead it saves the compiled package in the local build cache.
After confirming that the stringutil package builds, modify your original hello.go (which is in$GOPATH/src/github.com/user/hello) to use it:
package main

import (
 "fmt"

 "github.com/user/stringutil"
)

func main() {
 fmt.Println(stringutil.Reverse("!oG ,olleH"))
}
Install the hello program:
$ go install github.com/user/hello
Running the new version of the program, you should see a new, reversed message:
$ hello
Hello, Go!
After the steps above, your workspace should look like this:
bin/
    hello                 # command executable
src/
    github.com/user/
        hello/
            hello.go      # command source
        stringutil/
            reverse.go    # package source

Package names

The first statement in a Go source file must be
package name
where name is the package's default name for imports. (All files in a package must use the same name.)
Go's convention is that the package name is the last element of the import path: the package imported as "crypto/rot13" should be named rot13.
Executable commands must always use package main.
There is no requirement that package names be unique across all packages linked into a single binary, only that the import paths (their full file names) be unique.
See Effective Go to learn more about Go's naming conventions.

Testing

Go has a lightweight test framework composed of the go test command and the testing package.
You write a test by creating a file with a name ending in _test.go that contains functions named TestXXX with signature func (t *testing.T). The test framework runs each such function; if the function calls a failure function such as t.Error or t.Fail, the test is considered to have failed.
Add a test to the stringutil package by creating the file$GOPATH/src/github.com/user/stringutil/reverse_test.go containing the following Go code.
package stringutil

import "testing"

func TestReverse(t *testing.T) {
 cases := []struct {
  in, want string
 }{
  {"Hello, world", "dlrow ,olleH"},
  {"Hello, 世界", "界世 ,olleH"},
  {"", ""},
 }
 for _, c := range cases {
  got := Reverse(c.in)
  if got != c.want {
   t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want)
  }
 }
}
Then run the test with go test:
$ go test github.com/user/stringutil
ok   github.com/user/stringutil 0.165s
As always, if you are running the go tool from the package directory, you can omit the package path:
$ go test
ok   github.com/user/stringutil 0.165s
Run go help test and see the testing package documentation for more detail.

Remote packages

An import path can describe how to obtain the package source code using a revision control system such as Git or Mercurial. The go tool uses this property to automatically fetch packages from remote repositories. For instance, the examples described in this document are also kept in a Git repository hosted at GitHubgithub.com/golang/example. If you include the repository URL in the package's import path, go get will fetch, build, and install it automatically:
$ go get github.com/golang/example/hello
$ $GOPATH/bin/hello
Hello, Go examples!
If the specified package is not present in a workspace, go get will place it inside the first workspace specified by GOPATH. (If the package does already exist, go get skips the remote fetch and behaves the same as go install.)
After issuing the above go get command, the workspace directory tree should now look like this:
bin/
    hello                           # command executable
src/
    github.com/golang/example/
 .git/                       # Git repository metadata
        hello/
            hello.go                # command source
        stringutil/
            reverse.go              # package source
            reverse_test.go         # test source
    github.com/user/
        hello/
            hello.go                # command source
        stringutil/
            reverse.go              # package source
            reverse_test.go         # test source
The hello command hosted at GitHub depends on the stringutil package within the same repository. The imports in hello.go file use the same import path convention, so the go get command is able to locate and install the dependent package, too.
import "github.com/golang/example/stringutil"
This convention is the easiest way to make your Go packages available for others to use. The Go Wiki and godoc.org provide lists of external Go projects.
For more information on using remote repositories with the go tool, see go help importpath.

What's next

Subscribe to the golang-announce mailing list to be notified when a new stable version of Go is released.
See Effective Go for tips on writing clear, idiomatic Go code.
Take A Tour of Go to learn the language proper.
Visit the documentation page for a set of in-depth articles about the Go language and its libraries and tools.

Getting help

For real-time help, ask the helpful gophers in #go-nuts on the Freenode IRC server.
The official mailing list for discussion of the Go language is Go Nuts.
Report bugs using the Go issue tracker.

SettingGOPATH

The GOPATH environment variable specifies the location of your workspace. If no GOPATH is set, it is assumed to be $HOME/go on Unix systems and %USERPROFILE%\go on Windows. If you want to use a custom location as your workspace, you can set the GOPATH environment variable. This page explains how to set this variable on various platforms.

Unix systems

GOPATH can be any directory on your system. In Unix examples, we will set it to $HOME/go (the default since Go 1.8). Note that GOPATH must not be the same path as your Go installation. Another common setup is to set GOPATH=$HOME.

Bash

Edit your ~/.bash_profile to add the following line:
export GOPATH=$HOME/go
Save and exit your editor. Then, source your ~/.bash_profile.
source ~/.bash_profile

Zsh

Edit your ~/.zshrc file to add the following line:
export GOPATH=$HOME/go
Save and exit your editor. Then, source your ~/.zshrc.
source ~/.zshrc

fish

set -x -U GOPATH $HOME/go
The -x is used to specify that this variable should be exported and the -U makes this a universal variable, available to all sessions and persistent.

Windows

Your workspace can be located wherever you like, but we'll use C:\go-work in this example.
NOTE: GOPATH must not be the same path as your Go installation.
  • Create folder at C:\go-work.
  • Right click on "Start" and click on "Control Panel". Select "System and Security", then click on "System".
  • From the menu on the left, select the "Advanced systems settings".
  • Click the "Environment Variables" button at the bottom.
  • Click "New" from the "User variables" section.
  • Type GOPATH into the "Variable name" field.
  • Type C:\go-work into the "Variable value" field.
  • Click OK.

Windows 10

There is a faster way to edit Environment Variables via search:
  • Left click on "Search" and type env or environment.
  • Select "Edit environment variables for your account".
  • ... and follow steps above.

Windows 10 (cli version)

  • Open a command prompt (windows-key + r then type "cmd") or a powershell window (windows-key + i)
  • Type setx GOPATH %USERPROFILE%\go (this will set the GOPATH to your [home folder]\goe.g. C:\Users\yourusername\go
  • Close the command or powershell window (the environment variable is only available for new command or powershell windows, not for the current window).

Tuesday, January 22, 2019

Getting Started

Download the Go distribution

Official binary distributions are available for the FreeBSD (release 10-STABLE and above), Linux, macOS (10.10 and above), and Windows operating systems and the 32-bit (386) and 64-bit (amd64) x86 processor architectures.
If a binary distribution is not available for your combination of operating system and architecture, try installing from source or installing gccgo instead of gc.

System requirements

Go binary distributions are available for these supported operating systems and architectures. Please ensure your system meets these requirements before proceeding. If your OS or architecture is not on the list, you may be able to install from source or use gccgo instead.
Operating systemArchitecturesNotes

FreeBSD 10.3 or lateramd64, 386Debian GNU/kFreeBSD not supported
Linux 2.6.23 or later with glibcamd64, 386, arm, arm64,
s390x, ppc64le
CentOS/RHEL 5.x not supported.
Install from source for other libc.
macOS 10.10 or lateramd64use the clang or gcc that comes with Xcode for cgo support
Windows 7, Server 2008R2 or lateramd64, 386use MinGW gcc. No need for cygwin or msys.
A C compiler is required only if you plan to use cgo.
You only need to install the command line tools for Xcode. If you have already installed Xcode 4.3+, you can install it from the Components tab of the Downloads preferences panel.

Install the Go tools

If you are upgrading from an older version of Go you must first remove the existing version.

Linux, macOS, and FreeBSD tarballs

Download the archive and extract it into /usr/local, creating a Go tree in /usr/local/go. For example:
tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz
Choose the archive file appropriate for your installation. For instance, if you are installing Go version 1.2.1 for 64-bit x86 on Linux, the archive you want is called go1.2.1.linux-amd64.tar.gz.
(Typically these commands must be run as root or through sudo.)
Add /usr/local/go/bin to the PATH environment variable. You can do this by adding this line to your /etc/profile (for a system-wide installation) or $HOME/.profile:
export PATH=$PATH:/usr/local/go/bin
Note: changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as source $HOME/.profile.

macOS package installer

Download the package file, open it, and follow the prompts to install the Go tools. The package installs the Go distribution to /usr/local/go.
The package should put the /usr/local/go/bin directory in your PATH environment variable. You may need to restart any open Terminal sessions for the change to take effect.

Windows

The Go project provides two installation options for Windows users (besides installing from source): a zip archive that requires you to set some environment variables and an MSI installer that configures your installation automatically.

MSI installer

Open the MSI file and follow the prompts to install the Go tools. By default, the installer puts the Go distribution in c:\Go.
The installer should put the c:\Go\bin directory in your PATH environment variable. You may need to restart any open command prompts for the change to take effect.

Zip archive

Download the zip file and extract it into the directory of your choice (we suggest c:\Go).
If you chose a directory other than c:\Go, you must set the GOROOT environment variable to your chosen path.
Add the bin subdirectory of your Go root (for example, c:\Go\bin) to your PATH environment variable.

Setting environment variables under Windows

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.

Test your installation

Check that Go is installed correctly by setting up a workspace and building a simple program, as follows.
Create your workspace directory, $HOME/go. (If you'd like to use a different directory, you will need to set the GOPATH environment variable.)
Next, make the directory src/hello inside your workspace, and in that directory create a file named hello.gothat looks like:
package main

import "fmt"

func main() {
 fmt.Printf("hello, world\n")
}
Then build it with the go tool:
$ cd $HOME/go/src/hello
$ go build
The command above will build an executable named hello in the directory alongside your source code. Execute it to see the greeting:
$ ./hello
hello, world
If you see the "hello, world" message then your Go installation is working.
You can run go install to install the binary into your workspace's bin directory or go clean -i to remove it.
Before rushing off to write Go code please read the How to Write Go Code document, which describes some essential concepts about using the Go tools.

Uninstalling Go

To remove an existing Go installation from your system delete the go directory. This is usually /usr/local/gounder Linux, macOS, and FreeBSD or c:\Go under Windows.
You should also remove the Go bin directory from your PATH environment variable. Under Linux and FreeBSD you should edit /etc/profile or $HOME/.profile. If you installed Go with the macOS package then you should remove the /etc/paths.d/go file. Windows users should read the section about setting environment variables under Windows.

Getting help

Report bugs either by running “go bug”, or manually at the Go issue tracker.

Go (programming language)

  • Go (often referred to as Golang[13]) is a statically typedcompiled programming language designed at Google[14] by Robert Griesemer, Rob Pike, and Ken Thompson.[11] Go is syntactically similar to C, but with the added benefits of memory safetygarbage collectionstructural typing,[5] and CSP-style concurrency.[15]
    There are two major implementations:
    A third compiler, GopherJS,[21] compiles Go to JavaScript for front-end web development.

    History[edit]

    Go was designed at Google in 2007 to improve programming productivity in an era of multicorenetworked machines and extremely large codebases.[22] The designers wanted to address criticism of other languages in use at Google, while retaining their useful characteristics:[23]
    The designers were primarily motivated by their shared dislike of C++.[25][26][27]
    Go was publicly announced in November 2009,[28] and version 1.0 was released in March 2012.[29][30] Go is widely used in production at Google[31] and in many other organizations and open-source projects.
    Gopher mascot
    In April 2018, the original logo was replaced with a stylized GO slanting right with trailing streamlines. However, the gopher mascot remained the same.[32]
    In August 2018, the Go principal contributors published two ″draft designs″ for new language features, Generics and Error Handling, and asked Go users to submit feedback on them.[33][34] Lack of support for generic programming and the verbosity of error handling in Go 1.x had drawn considerable criticism.

    Version history[edit]

    Go 1 guarantees compatibility[35] for the language specification and major parts of the standard library. All versions up to the current Go 1.11 release[36] have maintained this promise.
    Each major Go release is supported until there are two newer major releases.[37]
    Major versionInitial release dateLanguage changes[38]Other changes
    1 - 1.0.32012/03/28Initial release
    1.1 - 1.1.22013/05/13
    • In Go 1.1, an integer division by constant zero is not a legal program, so it is a compile-time error.
    • The definition of string and rune literals has been refined to exclude surrogate halves from the set of valid Unicode code points.
    • Loosened return requirements rules. If the compiler can prove that a function always returns before reaching the end of a function, a final terminating statement can be omitted.
    • The language allows the implementation to choose whether the int type and uint types are 32 or 64 bits.
    • On 64-bit architectures, the maximum heap size has been enlarged substantially, from a few gigabytes to several tens of gigabytes.
    • Addition of a race detector to the standard tool set.
    1.2 - 1.2.22013/12/01
    • The language now specifies that, for safety reasons, certain uses of nil pointers are guaranteed to trigger a run-time panic.
    • Go 1.2 adds the ability to specify the capacity as well as the length when using a slicing operation on an existing array or slice. A slicing operation creates a new slice by describing a contiguous section of an already-created array or slice.
    • The runtime scheduler can now be invoked on (non-inlined) function calls.
    • Go 1.2 introduces a configurable limit (default 10,000) to the total number of threads a single program may have.
    • In Go 1.2, the minimum size of the stack when a goroutine is created has been lifted from 4KB to 8KB.
    1.3 - 1.3.32014/06/18There are no language changes in this release.
    • The Go 1.3 memory model adds a new rule concerning sending and receiving on buffered channels, to make explicit that a buffered channel can be used as a simple semaphore, using a send into the channel to acquire and a receive from the channel to release.
    • Go 1.3 has changed the implementation of goroutine stacks away from the old, "segmented" model to a contiguous model.
    • For a while now, the garbage collector has been precise when examining values in the heap; the Go 1.3 release adds equivalent precision to values on the stack.
    • Iterations over small maps no longer happen in a consistent order. This is due to developers abusing implementation behaviour.
    1.4 - 1.4.32014/12/10
    • Range-expression without assignment
    • Automatic double-dereference on method calls is now disallowed in gc and gccgo. This is a backwards incompatible change but inline within the language specification.
    • In 1.4, much of the runtime code has been translated to Go so that the garbage collector can scan the stacks of programs in the runtime and get accurate information about what variables are active.
    • The language accepted by the assemblers cmd/5acmd/6a and cmd/8a has had several changes, mostly to make it easier to deliver type information to the runtime.
    • Addition of internal packages.
    • New subcommand go generate.
    1.5 - 1.5.42015/08/19
    • Due to an oversight, the rule that allowed the element type to be elided from slice literals was not applied to map keys. This has been corrected in Go 1.5.
    • The compiler and runtime are now implemented in Go and assembler, without C. Now that the Go compiler and runtime are implemented in Go, a Go compiler must be available to compile the distribution from source. The compiler is now self-hosted.
    • The garbage collector has been re-engineered for 1.5. The "stop the world" phase of the collector will almost always be under 10 milliseconds and usually much less.
    • In Go 1.5, the order in which goroutines are scheduled has been changed.
    1.6 - 1.6.42016/02/17There are no language changes in this release.
    • A major change was made to cgo defining the rules for sharing Go pointers with C code, to ensure that such C code can coexist with Go's garbage collector.
    • The Go parser is now hand-written instead of generated.
    • The go vet command now diagnoses passing function or method values as arguments to Printf, such as when passing f where f() was intended.
    1.7 - 1.7.62016/08/15
    • Clarification on terminating statements in the language specification. This does not change existing behaviour.
    • For 64-bit x86 systems, the following instructions have been added: PCMPESTRIRORXLRORXQVINSERTI128VPADDDVPADDQVPALIGNRVPBLENDDVPERM2F128VPERM2I128VPORVPSHUFBVPSHUFDVPSLLDVPSLLDQVPSLLQVPSRLDVPSRLDQ, and VPSRLQ.
    • This release includes a new code generation back end for 64-bit x86 systems, based on SSA.
    • Packages using cgo may now include Fortran source files (in addition to C, C++, Objective C, and SWIG), although the Go bindings must still use C language APIs.
    • The new subcommand “go tool dist list” prints all supported operating system/architecture pairs.
    1.8 - 1.8.72017/02/16
    • When explicitly converting a value from one struct type to another, as of Go 1.8 the tags are ignored. Thus two structs that differ only in their tags may be converted from one to the other.
    • For 64-bit x86 systems, the following instructions have been added: VBROADCASTSDBROADCASTSSMOVDDUPMOVSHDUPMOVSLDUPVMOVDDUPVMOVSHDUP, and VMOVSLDUP.
    • Garbage collection pauses should be significantly shorter than they were in Go 1.7, usually under 100 microseconds and often as low as 10 microseconds. See the document on eliminating stop-the-world stack re-scanning for details.
    • The overhead of deferred function calls has been reduced by about half.
    • The overhead of calls from Go into C has been reduced by about half.
    1.9 - 1.9.72017/08/24
    • Go now supports type aliases.
    • Force the intermediate rounding in floating-point arithmetic.
    • The Go compiler now supports compiling a package's functions in parallel, taking advantage of multiple cores.
    1.10 - 1.10.72018/02/16
    • A corner case involving shifts of untyped constants has been clarified.
    • The grammar for method expressions has been updated to relax the syntax to allow any type expression as a receiver.
    • For the X86 64-bit port, the assembler now supports 359 new instructions, including the full AVX, AVX2, BMI, BMI2, F16C, FMA3, SSE2, SSE3, SSSE3, SSE4.1, and SSE4.2 extension sets. The assembler also no longer implements MOVL $0, AX as an XORL instruction, to avoid clearing the condition flags unexpectedly.
    1.11 - 1.11.42018/08/24There are no changes to the language specification.
    • Go 1.11 adds an experimental port to WebAssembly.
    • Go 1.11 adds preliminary support for a new concept called “modules,” an alternative to GOPATH with integrated support for versioning and package distribution.
    • The assembler for amd64 now accepts AVX512 instructions.
    • Go 1.11 drops support of Windows XP and Windows Vista.[39]
    • Go 1.11.3 and later fix the TLS authentication vulnerability in the crypto/x509 package.[40]

    Design[edit]

    Go is recognizably in the tradition of C, but makes many changes to improve brevity, simplicity, and safety. The language consists of:

    Syntax[edit]

    Go's syntax includes changes from C aimed at keeping code concise and readable. A combined declaration/initialization operator was introduced that allows the programmer to write i := 3 or s := "Hello, world!"without specifying the types of variables. This contrasts with C's int i = 3; and const char *s = "Hello, world!";. Semicolons still terminate statements,[a] but are implicit when the end of a line occurs.[b] Functions may return multiple values, and returning a result, err pair is the conventional way a function indicates an error to its caller in Go.[c] Go adds literal syntaxes for initializing struct parameters by name, and for initializing maps and slices. As an alternative to C's three-statement for loop, Go's range expressions allow concise iteration over arrays, slices, strings, maps, and channels.[citation needed]

    Types[edit]

    Go has a number of built-in types, including numeric ones (byteint64float32, etc.), booleans, and character strings (string). Strings are immutable; built-in operators, and keywords (rather than functions) provide concatenation, comparison, and UTF-8encoding/decoding.[48] Record types can be defined with the struct keyword.[49]
    For each type T and each non-negative integer constant n, there is an array type denoted [n]T; arrays of differing lengths are thus of different types. Dynamic arrays are available as "slices", denoted []T for some type T. These have a length and a capacity specifying when new memory needs to be allocated to expand the array. Several slices may share their underlying memory.[50][51][52]
    Pointers are available for all types, and the pointer-to-T type is denoted *T. Address-taking and indirection use the & and * operators as in C, or happen implicitly through the method call or attribute access syntax.[53] There is no pointer arithmetic,[d] except via the special unsafe.Pointer type in the standard library.[54]
    For a pair of types KV, the type map[K]V is the type of hash tables mapping type-K keys to type-V values. Hash tables are built into the language, with special syntax and built-in functions. chan T is a channel that allows sending values of type T between concurrent Go processes.[citation needed]
    Aside from its support for interfaces, Go's type system is nominal: the type keyword can be used to define a new named type, which is distinct from other named types that have the same layout (in the case of a struct, the same members in the same order). Some conversions between types (e.g., between the various integer types) are pre-defined and adding a new type may define additional conversions, but conversions between named types must always be invoked explicitly.[55] For example, the type keyword can be used to define a type for IPv4 addresses, based on 32-bit unsigned integers.[citation needed]
    type ipv4addr uint32
    
    With this type definition, ipv4addr(x) interprets the uint32 value x as an IP address. Simply assigning x to a variable of type ipv4addr is a type error.[citation needed]
    Constant expressions may be either typed or "untyped"; they are given a type when assigned to a typed variable if the value they represent passes a compile-time check.[56]
    Function types are indicated by the func keyword; they take zero or more parameters and return zero or more values, all of which are typed. The parameter and return values determine a function type; thus, func(string, int32) (int, error) is the type of functions that take a string and a 32-bit signed integer, and return a signed integer (of default width) and a value of the built-in interface type error.[citation needed]
    Any named type has a method set associated with it. The IP address example above can be extended with a method for checking whether its value is a known standard.
    // ZeroBroadcast reports whether addr is 255.255.255.255.
    func (addr ipv4addr) ZeroBroadcast() bool {
        return addr == 0xFFFFFFFF
    }
    
    Due to nominal typing, this method definition adds a method to ipv4addr, but not on uint32. While methods have special definition and call syntax, there is no distinct method type.[57]

    Interface system[edit]

    Go provides two features that replace class inheritance.[citation needed]
    The first is embedding, which can be viewed as an automated form of composition[58] or delegation.[59]:255
    The second are its interfaces, which provides runtime polymorphism.[60]:266 Interfaces are a class of types and provide a limited form of structural typing in the otherwise nominal type system of Go. An object which is of an interface type is also of another type, much like C++ objects being simultaneously of a base and derived class. Go interfaces were designed after protocols from the Smalltalk programming language.[61] Multiple sources use the term duck typing when describing Go interfaces.[62][63] Although the term duck typing is not precisely defined and therefore not wrong, it usually implies that type conformance is not statically checked. Since conformance to a Go interface is checked statically by the Go compiler (except when performing a type assertion), the Go authors prefer the term structural typing.[64]
    The definition of an interface type lists required methods by name and type. Any object of type T for which functions exist matching all the required methods of interface type I is an object of type I as well. The definition of type T need not (and cannot) identify type I. For example, if ShapeSquare and Circle are defined as:
    import "math"
    
    type Shape interface {
        Area() float64
    }
    
    type Square struct { // Note: no "implements" declaration
        side float64
    }
    
    func (sq Square) Area() float64 { return sq.side * sq.side }
    
    type Circle struct { // No "implements" declaration here either
        radius float64
    }
    
    func (c Circle) Area() float64 { return math.Pi * math.Pow(c.radius, 2) }
    
    Both a Square and a Circle are implicitly a Shape and can be assigned to a Shape-typed variable.[60]:263–268 In formal language, Go's interface system provides structural rather than nominal typing. Interfaces can embed other interfaces with the effect of creating a combined interface that is satisfied by exactly the types that implement the embedded interface and any methods that the newly defined interface adds.[60]:270
    The Go standard library uses interfaces to provide genericity in several places, including the input/output system that is based on the concepts of Reader and Writer.[60]:282–283
    Besides calling methods via interfaces, Go allows converting interface values to other types with a run-time type check. The language constructs to do so are the type assertion,[65] which checks against a single potential type, and the type switch,[66] which checks against multiple types.[citation needed]
    The empty interface interface{} is an important base case because it can refer to an item of any concrete type. It is similar to the Object class in Java or C# and is satisfied by any type, including built-in types like int.[60]:284 Code using the empty interface cannot simply call methods (or built-in operators) on the referred-to object, but it can store the interface{} value, try to convert it to a more useful type via a type assertion or type switch, or inspect it with Go's reflect package.[67] Because interface{} can refer to any value, it is a limited way to escape the restrictions of static typing, like void* in C but with additional run-time type checks.[citation needed]
    Interface values are implemented using pointer to data and a second pointer to run-time type information.[68] Like some other types implemented using pointers in Go, interface values are nil if uninitialized.[69]

    Package system[edit]

    In Go's package system, each package has a path (e.g., "compress/bzip2" or "golang.org/x/net/html") and a name (e.g., bzip2 or html). References to other packages' definitions must always be prefixed with the other package's name, and only the capitalized names from other packages are accessible: io.Reader is public but bzip2.reader is not.[70] The go get command can retrieve packages stored in a remote repository [71] and developers are encouraged to develop packages inside a base path corresponding to a source repository (such as example.com/user_name/package_name) to reduce the likelihood of name collision with future additions to the standard library or other external libraries.[72]
    Proposals exist to introduce a proper package management solution for Go similar to Rust's cargo system or Node's npm system.[73]

    Concurrency: goroutines and channels[edit]

    The Go language has built-in facilities, as well as library support, for writing concurrent programs. Concurrency refers not only to CPU parallelism, but also to asynchrony: letting slow operations like a database or network-read run while the program does other work, as is common in event-based servers.[74]
    The primary concurrency construct is the goroutine, a type of light-weight process. A function call prefixed with the go keyword starts a function in a new goroutine. The language specification does not specify how goroutines should be implemented, but current implementations multiplex a Go process's goroutines onto a smaller set of operating system threads, similar to the scheduling performed in Erlang.[75]:10
    While a standard library package featuring most of the classical concurrency control structures (mutex locks, etc.) is available,[75]:151–152 idiomatic concurrent programs instead prefer channels, which provide send messages between goroutines.[76] Optional buffers store messages in FIFO order[59]:43 and allow sending goroutines to proceed before their messages are received.[citation needed]
    Channels are typed, so that a channel of type chan T can only be used to transfer messages of type T. Special syntax is used to operate on them; <-ch is an expression that causes the executing goroutine to block until a value comes in over the channel ch, while ch <- x sends the value x (possibly blocking until another goroutine receives the value). The built-in switch-like select statement can be used to implement non-blocking communication on multiple channels; see below for an example. Go has a memory model describing how goroutines must use channels or other operations to safely share data.[77]
    The existence of channels sets Go apart from actor model-style concurrent languages like Erlang, where messages are addressed directly to actors (corresponding to goroutines). The actor style can be simulated in Go by maintaining a one-to-one correspondence between goroutines and channels, but the language allows multiple goroutines to share a channel or a single goroutine to send and receive on multiple channels.[75]:147
    From these tools one can build concurrent constructs like worker pools, pipelines (in which, say, a file is decompressed and parsed as it downloads), background calls with timeout, "fan-out" parallel calls to a set of services, and others.[78] Channels have also found uses further from the usual notion of interprocess communication, like serving as a concurrency-safe list of recycled buffers,[79] implementing coroutines (which helped inspire the name goroutine),[80] and implementing iterators.[81]
    Concurrency-related structural conventions of Go (channels and alternative channel inputs) are derived from Tony Hoare's communicating sequential processes model. Unlike previous concurrent programming languages such as Occam or Limbo (a language on which Go co-designer Rob Pike worked),[82] Go does not provide any built-in notion of safe or verifiable concurrency.[83] While the communicating-processes model is favored in Go, it is not the only one: all goroutines in a program share a single address space. This means that mutable objects and pointers can be shared between goroutines; see § Lack of race condition safety, below.[citation needed]

    Suitability for parallel programming[edit]

    Although Go's concurrency features are not aimed primarily at parallel processing,[74] they can be used to program shared memory multi-processor machines. Various studies have been done into the effectiveness of this approach.[84] One of these studies compared the size (in lines of code) and speed of programs written by a seasoned programmer not familiar with the language and corrections to these programs by a Go expert (from Google's development team), doing the same for ChapelCilk and Intel TBB. The study found that the non-expert tended to write divide-and-conquer algorithms with one go statement per recursion, while the expert wrote distribute-work-synchronize programs using one goroutine per processor. The expert's programs were usually faster, but also longer.[85]

    Lack of race condition safety[edit]

    There are no restrictions on how goroutines access shared data, making race conditions possible. Specifically, unless a program explicitly synchronizes via channels or other means, writes from one goroutine might be partly, entirely, or not at all visible to another, often with no guarantees about ordering of writes.[83] Furthermore, Go's internal data structures like interface values, slice headers, hash tables, and string headers are not immune to race conditions, so type and memory safety can be violated in multithreaded programs that modify shared instances of those types without synchronization.[86][87] Instead of language support, safe concurrent programming thus relies on conventions; for example, Chisnall recommends an idiom called "aliases xor mutable", meaning that passing a mutable value (or pointer) over a channel signals a transfer of ownership over the value to its receiver.[75]:155

    Binaries[edit]

    The linker in the gc toolchain creates statically-linked binaries by default therefore all Go binaries include the Go runtime.[88][89]

    Omissions[edit]

    Go deliberately omits certain features common in other languages, including (implementation) inheritancegeneric programmingassertions,[e] pointer arithmetic,[d] implicit type conversionsuntagged unions,[f] and tagged unions.[g] The designers added only those facilities that all three agreed on.[92]
    Of the omitted language features, the designers explicitly argue against assertions and pointer arithmetic, while defending the choice to omit type inheritance as giving a more useful language, encouraging instead the use of interfaces to achieve dynamic dispatch[h] and composition to reuse code. Composition and delegation are in fact largely automated by struct embedding; according to researchers Schmager et al., this feature "has many of the drawbacks of inheritance: it affects the public interface of objects, it is not fine-grained (i.e, no method-level control over embedding), methods of embedded objects cannot be hidden, and it is static", making it "not obvious" whether programmers will overuse it to the extent that programmers in other languages are reputed to overuse inheritance.[58]
    The designers express an openness to generic programming and note that built-in functions are in fact type-generic, but these are treated as special cases; Pike calls this a weakness that may at some point be changed.[50] The Google team built at least one compiler for an experimental Go dialect with generics, but did not release it.[93] They are also open to standardizing ways to apply code generation.[94]
    Initially omitted, the exception-like panic/recover mechanism was eventually added, which the Go authors advise using for unrecoverable errors such as those that should halt an entire program or server request, or as a shortcut to propagate errors up the stack within a package (but not across package boundaries; there, error returns are the standard API).[95][96][97][98]

    Criticisms[edit]

    Go critics assert that:
    The designers argue these omissions were simplifications that contribute to Go's strength[103] (See § Omissions above).

    Style[edit]

    The Go authors put substantial effort into influencing the style of Go programs:
    • Indentation, spacing, and other surface-level details of code are automatically standardized by the gofmt tool. golint does additional style checks automatically.
    • Tools and libraries distributed with Go suggest standard approaches to things like API documentation (godoc),[104] testing (go test), building (go build), package management (go get), and so on.
    • Go enforces rules that are recommendations in other languages, for example banning cyclic dependencies, unused variables or imports, and implicit type conversions.
    • The omission of certain features (for example, functional-programming shortcuts like map and Java-style try/finally blocks) tends to encourage a particular explicit, concrete, and imperative programming style.
    • On day one the Go team published a collection of Go idioms,[104] and later also collected code review comments,[105] talks,[106] and official blog posts[107] to teach Go style and coding philosophy.

    Tools[edit]

    The main Go distribution includes tools for buildingtesting, and analyzing code:
    • go build, which builds Go binaries using only information in the source files themselves, no separate makefiles
    • go test, for unit testing and microbenchmarks
    • go fmt, for formatting code
    • go get, for retrieving and installing remote packages
    • go vet, a static analyzer looking for potential errors in code
    • go run, a shortcut for building and executing code
    • godoc, for displaying documentation or serving it via HTTP
    • gorename, for renaming variables, functions, and so on in a type-safe way
    • go generate, a standard way to invoke code generators
    It also includes profiling and debugging support, runtime instrumentation (for example, to track garbage collection pauses), and a race condition tester.
    An ecosystem of third-party tools adds to the standard distribution, such as gocode, which enables code autocompletion in many text editors, goimports (by a Go team member), which automatically adds/removes package imports as needed, and errcheck, which detects code that might unintentionally ignore errors. Plugins exist for adding language support to several text editors. Several IDEs are available, including LiteIDE, a "simple, open source, cross-platform Go IDE",[108] and GoLand, which claims to be "capable and ergonomic."[109]

    Examples[edit]

    Hello world[edit]

    package main
    
    import "fmt"
    
    func main() {
        fmt.Println("Hello, world")
    }
    

    Concurrency[edit]

    The following simple program demonstrates Go's concurrency features to implement an asynchronous program. It launches two "goroutines" (lightweight threads): one waits for the user to type some text, while the other implements a timeout. The select statement waits for either of these goroutines to send a message to the main routine, and acts on the first message to arrive (example adapted from David Chisnall book).[75]:152
    package main
    
    import (
        "fmt"
        "time"
    )
    
    func readword(ch chan string) {
        fmt.Println("Type a word, then hit Enter.")
        var word string
        fmt.Scanf("%s", &word)
        ch <- word
    }
    
    func timeout(t chan bool) {
        time.Sleep(5 * time.Second)
        t <- false
    }
    
    func main() {
        t := make(chan bool)
        go timeout(t)
    
        ch := make(chan string)
        go readword(ch)
    
        select {
        case word := <-ch:
            fmt.Println("Received", word)
        case <-t:
            fmt.Println("Timeout.")
        }
    }
    

    Applications[edit]

    Some notable open-source applications in Go include:[110]
    • Caddy, an open source HTTP/2 web server with automatic HTTPS capability.
    • CockroachDB, an open source, survivable, strongly consistent, scale-out SQL database.
    • Docker, a set of tools for deploying Linux containers
    • Ethereum, The go-ethereum implementation of the Ethereum Virtual Machine blockchain for the Ether cryptocurrency [111]
    • Hugo, a static site generator
    • InfluxDB, an open source database specifically to handle time series data with high availability and high performance requirements.
    • InterPlanetary File System, a content-addressable, peer-to-peer hypermedia protocol.[112]
    • Juju, a service orchestration tool by Canonical, packagers of Ubuntu Linux
    • Kubernetes container management system
    • Lightning Network, a bitcoin network that allows for fast Bitcoin transactions and scalability.[113]
    • Mattermost, a teamchat system
    • OpenShift, a cloud computing platform as a service by Red Hat
    • Snappy, a package manager for Ubuntu Touch developed by Canonical.
    • Syncthing, an open-source file synchronization client/server application
    • Terraform, an open-source, multiple cloud infrastructure provisioning tool from HashiCorp.
    Other notable companies and sites using Go (generally together with other languages, not exclusively) include:

    Reception[edit]

    The interface system, and the deliberate omission of inheritance, were praised by Michele Simionato, who likened these characteristics to those of Standard ML, calling it "a shame that no popular language has followed [this] particular route".[137]
    Dave Astels at Engine Yard wrote:[138]
    Go is extremely easy to dive into. There are a minimal number of fundamental language concepts and the syntax is clean and designed to be clear and unambiguous.
    Go is still experimental and still a little rough around the edges.
    Go was named Programming Language of the Year by the TIOBE Programming Community Index in its first year, 2009, for having a larger 12-month increase in popularity (in only 2 months, after its introduction in November) than any other language that year, and reached 13th place by January 2010,[139] surpassing established languages like Pascal. By June 2015, its ranking had dropped to below 50th in the index, placing it lower than COBOL and Fortran.[140] But as of January 2017, its ranking had surged to 13th, indicating significant growth in popularity and adoption. Go was awarded TIOBE programming language of the year 2016.
    Bruce Eckel has stated:[141]
    The complexity of C++ (even more complexity has been added in the new C++), and the resulting impact on productivity, is no longer justified. All the hoops that the C++ programmer had to jump through in order to use a C-compatible language make no sense anymore -- they're just a waste of time and effort. Go makes much more sense for the class of problems that C++ was originally intended to solve.
    A 2011 evaluation of the language and its gc implementation in comparison to C++ (GCC), Java and Scala by a Google engineer found:
    Go offers interesting language features, which also allow for a concise and standardized notation. The compilers for this language are still immature, which reflects in both performance and binary sizes.
    — R. Hundt[142]
    The evaluation got a rebuttal from the Go development team. Ian Lance Taylor, who had improved the Go code for Hundt's paper, had not been aware of the intention to publish his code, and says that his version was "never intended to be an example of idiomatic or efficient Go"; Russ Cox then did optimize the Go code, as well as the C++ code, and got the Go code to run slightly faster than C++ and more than an order of magnitude faster than the code in the paper.[143]

    Naming dispute[edit]

    On November 10, 2009, the day of the general release of the language, Francis McCabe, developer of the Go! programming language (note the exclamation point), requested a name change of Google's language to prevent confusion with his language, which he had spent 10 years developing.[144] McCabe raised concerns that "the 'big guy' will end up steam-rollering over" him, and this concern resonated with the more than 120 developers who commented on Google's official issues thread saying they should change the name, with some[145] even saying the issue contradicts Google's motto of: Don't be evil.[146]
    On October 12, 2010, the issue was closed by Google developer Russ Cox (@rsc) with the custom status "Unfortunate" accompanied by the following comment:
    "There are many computing products and services named Go. In the 11 months since our release, there has been minimal confusion of the two languages."[146]