Introduction

This document explains how to write a new package, how to test code, and how to contribute changes to the Go project. It assumes you have installed Go using the installation instructions. (Note that the gccgo frontend lives elsewhere; see Contributing to gccgo.)

Before embarking on a significant change to an existing package or the creation of a major new package, it's a good idea to send mail to the mailing list to let people know what you are thinking of doing. Doing so helps avoid duplication of effort and enables discussions about design before much code has been written.

Community resources

For real-time help, there may be users or developers on #go-nuts on the Freenode IRC server.

The official mailing list for discussion of the Go language is Go Nuts.

Bugs can be reported using the Go issue tracker.

For those who wish to keep up with development, there is another mailing list, golang-checkins, that receives a message summarizing each checkin to the Go repository.

Creating a new package

The source code for the package with import path x/y is, by convention, kept in the directory $GOROOT/src/pkg/x/y.

Makefile

It would be nice to have Go-specific tools that inspect the source files to determine what to build and in what order, but for now, Go uses GNU make. Thus, the first file to create in a new package directory is usually the Makefile. The basic form is illustrated by src/pkg/container/vector/Makefile:

include $(GOROOT)/src/Make.$(GOARCH)

TARG=container/vector
GOFILES=\
	intvector.go\
	stringvector.go\
	vector.go\

include $(GOROOT)/src/Make.pkg

The first and last lines include standard definitions and rules, so that the body of the Makefile need only specify two variables.

TARG is the target install path for the package, the string that clients will use to import it. This string should be the same as the directory in which the Makefile appears, with the $GOROOT/src/pkg/ removed.

GOFILES is a list of source files to compile to create the package. The trailing \ characters allow the list to be split onto multiple lines for easy sorting.

After creating a new package directory, add it to the list in $GOROOT/src/pkg/Makefile so that it is included in the standard build. Then run:

cd $GOROOT/src/pkg
./deps.bash

to update the dependency file Make.deps.

If you change the imports of an existing package, you do not need to edit $GOROOT/src/pkg/Makefile but you will still need to run deps.bash as above.

Go source files

The first statement in each of the source files listed in the Makefile should 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. The Go tools impose a restriction that package names are unique across all packages linked into a single binary, but that restriction will be lifted soon.

Go compiles all the source files in a package at once, so one file can refer to constants, variables, types, and functions in another file without special arrangement or declarations.

Writing clean, idiomatic Go code is beyond the scope of this document. Effective Go is an introduction to that topic.

Testing

Go has a lightweight test framework known as gotest. 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. The gotest command documentation and the testing package documentation give more detail.

The *_test.go files should not be listed in the Makefile.

To run the test, run either make test or gotest (they are equivalent). To run only the tests in a single test file, for instance one_test.go, run gotest one_test.go.

Before sending code out for review, make sure everything still works and the dependencies are right:

cd $GOROOT/src
./all.bash

The final line printed by all.bash should be of the form:

N known bugs; 0 unexpected bugs

The value of N varies over time, but the line must say “0 unexpected bugs” and must not add “test output differs.”

Once your new code is tested and working, it's time to get it reviewed and submitted.

Code review

Changes to Go must be reviewed before they are submitted, no matter who makes the change. (In exceptional cases, such as fixing a build, the review can follow shortly after submitting.) A Mercurial extension helps manage the code review process. The extension is included in the Go source tree but needs to be added to your Mercurial configuration.

Caveat for Mercurial aficionados

Using Mercurial with the code review extension is not the same as using standard Mercurial.

The Go repository is maintained as a single line of reviewed changes; we prefer to avoid the complexity of Mercurial's arbitrary change graph. The code review extension helps here: its hg submit command automatically checks for and warns about the local repository being out of date compared to the remote one. The hg submit command also verifies other properties about the Go repository. For example, it checks that Go code being checked in is formatted in the standard style, as defined by gofmt, and it checks that the author of the code is properly recorded for copyright purposes.

To help ensure changes are only created by hg submit, the code review extension disables the standard hg commit command.

Mercurial power users: To allow Go contributors to take advantage of Mercurial's functionality for local revision control, it might be interesting to explore how the code review extension can be made to work alongside the Mercurial Queues extension.

Configure the extension

[NOTE FOR BEFORE LAUNCH: Read this instead.]

Edit $GOROOT/.hg/hgrc to add:

[extensions]
codereview = YOUR_GO_ROOT/lib/codereview/codereview.py

Replace YOUR_GO_ROOT with the value of $GOROOT. The Mercurial configuration file format does not allow environment variable substitution.

Log in to the code review site.

[NOTE FOR BEFORE LAUNCH: Read this instead.]

The code review server uses a Google Account to authenticate. (If you can use the account to sign in at google.com, you can use it to sign in to the code review server.)

$ cd $GOROOT
$ hg code-login
Email (login for uploading to codereview.appspot.com): rsc@golang.org
Password for rsc@golang.org:

Saving authentication cookies to /Users/rsc/.codereview_upload_cookies_codereview.appspot.com

Configure your account settings.

Edit your code review settings. Grab a nickname. Many people prefer to set the Context option to “Whole file” to see more context when reviewing changes.

Once you have chosen a nickname in the settings page, others can use that nickname as a shorthand for naming reviewers and the CC list. For example, rsc is an alias for rsc@golang.org.

Make a change

The entire checked-out tree is writable. If you need to edit files, just edit them: Mercurial will figure out which ones changed. You do need to inform Mercurial of added, removed, copied, or renamed files, by running hg add, hg rm, hg cp, or hg mv.

When you are ready to send a change out for review, run

$ hg change

from any directory in your Go repository. Mercurial will open a change description file in your editor. (It uses the editor named by the $EDITOR environment variable, vi by default.) The file will look like:

# Change list.
# Lines beginning with # are ignored.
# Multi-line values should be indented.

Reviewer:
CC:

Description:
	<enter description here>

Files:
	src/pkg/math/sin.go
	src/pkg/math/tan.go
	src/pkg/regexp/regexp.go

The Reviewer line lists the reviewers assigned to this change, and the CC line lists people to notify about the change. These can be code review nicknames or arbitrary email addresses.

Replace “<enter description here>” with a description of your change. The first line of the change description is conventionally a one-line summary of the change and is used as the subject for code review mail; the rest of the description elaborates.

The Files section lists all the modified files in your client. It is best to keep unrelated changes in different change lists. In this example, we can include just the changes to package math by deleting the line mentioning regexp.go.

After editing, the template might now read:

# Change list.
# Lines beginning with # are ignored.
# Multi-line values should be indented.

Reviewer: r, rsc
CC: math-nuts@swtch.com

Description:
	Sin, Cos, Tan: improved precision for very large arguments

	See Bimmler and Shaney, ``Extreme sinusoids,'' J. Math 3(14).
	Fixes issue 159.

Files:
	src/pkg/math/sin.go
	src/pkg/math/tan.go

The special sentence “Fixes issue 159.” associates the change with issue 159 in the Go issue tracker. When this change is eventually submitted, the issue tracker will automatically mark the issue as fixed.

Save the file and exit the editor.

The code review server assigns your change an issue number and URL, which hg change will print, something like:

CL created: http://codereview.appspot.com/99999

If you need to re-edit the change description, run hg change 99999.

You can see a list of your pending changes by running hg pending (hg p for short).

Synchronize your client

While you were working, others might have submitted changes to the repository. To update your client, run

$ hg sync

(For Mercurial fans, hg sync runs hg pull -u but then also synchronizes the local change list state against the new data.)

If files you were editing have changed, Mercurial does its best to merge the remote changes into your local changes. It may leave some files to merge by hand.

For example, suppose you have edited flag_test.go but someone else has committed an independent change. When you run hg sync, you will get the (scary-looking) output (emphasis added):

$ hg sync
adding changesets
adding manifests
adding file changes
added 1 changeset with 2 changes to 2 files
getting src/pkg/flag/flag.go
couldn't find merge tool hgmerge
merging src/pkg/flag/flag_test.go
warning: conflicts during merge.
merging src/pkg/flag/flag_test.go failed!
1 file updated, 0 files merged, 0 files removed, 1 file unresolved
use 'hg resolve' to retry unresolved file merges
$

The only important part in that transcript is the italicized line: Mercurial failed to merge your changes with the independent change. When this happens, Mercurial leaves both edits in the file, marked by <<<<<<< and >>>>>>>. it is now your job to edit the file to combine them. Continuing the example, searching for those strings in flag_test.go might turn up:

	VisitAll(visitor);
<<<<<<< local
	if len(m) != 7 {
=======
	if len(m) != 8 {
>>>>>>> other
		t.Error("VisitAll misses some flags");

Mercurial doesn't show it, but suppose the original text that both edits started with was 6; you added 1 and the other change added 2, so the correct answer might now be 9. If you edit the section to remove the markers and leave the correct code:

	VisitAll(visitor);
	if len(m) != 9 {
		t.Error("VisitAll misses some flags");

then that is enough. There is no need to inform Mercurial that you have corrected the file.

If you had been editing the file, say for debugging, but do not care to preserve your changes, you can run hg revert flag_test.go to abandon your changes.

Mail the change for review

To send out a change for review, run hg mail using the change list number assigned during hg change:

$ hg mail 99999

You can add to the Reviewer: and CC: lines using the -r or --cc options. In the above example, we could have left the Reviewer and CC lines blank and then run:

$ hg mail -r r,rsc --cc math-nuts@swtch.com 99999

to achieve the same effect.

Note that -r and --cc cannot be spelled --r or -cc.

Reviewing code

Running hg mail will send an email to you and the reviewers asking them to visit the issue's URL and make coments on the change. When done, the reviewer clicks “Publish and Mail comments” to send comments back.

Revise and upload

You will probably revise your code in response to the reviewer comments. When you have revised the code and are ready for another round of review, run

$ hg upload 99999

to upload the latest copy. You might also visit the code review web page and reply to the comments, letting the reviewer know that you've addressed them or explain why you haven't. When you're done replying, click “Publish and Mail comments” to send the line-by-line replies and any other comments. A common acronym in such mails is PTAL: please take another look.

The reviewer can comment on the new copy, and the process repeats. The reviewer approves the change by replying with a mail that says LGTM: looks good to me.

Submit the change

Once the code has been LGTM'ed, it is time to submit it to the Mercurial repository. If you are a committer, you can run:

$ hg submit 99999

This checks the change into the repository. The change description will include a link to the code review, and the code review will be updated with a link to the change in the repository.

If your local copy of the repository is out of date, hg submit will refuse the change:

$ hg submit 12345678
local repository out of date; must sync before submit

If you are not a committer, you cannot submit the change directly. Instead, a committer, usually the reviewer who said LGTM, will run:

$ hg clpatch 99999
$ hg submit 99999

The clpatch command imports your change 99999 into the committer's local Mercurial client, at which point the committer can check or test the code more. (Anyone can run clpatch to try a change that has been uploaded to the code review server.) The submit command submits the code. You will be listed as the author, but the change message will also indicate who the committer was. Your local client will notice that the change has been submitted when you next run hg sync.

The standard copyright header for files in the Go tree is:

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Code you contribute should have this header. You need to be listed in the CONTRIBUTORS file, which defines who the Go contributors—the people—are; and the copyright holder for the code you submit (either you or the organization you work for) needs to be listed in the AUTHORS file, which defines who “The Go Authors”—the copyright holders—are.

When sending your first change list, you should prepare and send a separate change list adding yourself to CONTRIBUTORS and adding the copyright holder for your code to AUTHORS if not already listed. If you are the copyright holder, you will need to agree to the individual contributor license agreement, which can be completed online; if your organization is the copyright holder, the organization will need to agree to the corporate contributor license agreement. If the copyright holder for your code has already completed the agreement in connection with another Google open source project, it does not need to be completed again. One of the Go developers at Google will approve and submit this change after checking the list of people/organizations that have completed the agreement.