Execute Formate

Contribution Guidelines


This document explains how to contribute changes to the Go project. It assumes you have installed Go using the installation instructions and have written and tested your code. (Note that the gccgo frontend lives elsewhere; see Contributing to gccgo.)

Discuss your design

The project welcomes submissions but please let everyone know what you're working on if you want it to become part of the main repository.

Before undertaking to write something new for the Go project, send mail to the mailing list to discuss what you plan to do. This gives everyone a chance to validate the design, helps prevent duplication of effort, and ensures that the idea fits inside the goals for the language and tools. It also guarantees that the design is sound before code is written; the code review tool is not the place for high-level discussions.

In short, send mail before you code. And don't start the discussion by mailing a change list!

Testing redux

You've written and tested your code, but before sending code out for review, run all the tests for the whole tree to make sure the changes don't break other packages or programs:

cd $GOROOT/src
./all.bash    # On Windows, run all.bat

After running for a while, the command should print "ALL TESTS PASSED".

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.

Configure the extension

Edit $GOROOT/.hg/hgrc to add:

codereview = $GOROOT/lib/codereview/codereview.py

username = Your Name <you@server.dom>

The username information will not be used unless you are a committer (see below), but Mercurial complains if it is missing.

Understanding the extension

After adding the code review extension, you can run

$ hg help codereview

to learn more about its commands. To learn about a specific code-review-specific command such as change, run

$ hg help change

As the codereview extension is only enabled for your checkout in $GOROOT, the remainder of this document assumes you are inside $GOROOT when issuing commands.

Windows users may need to perform extra steps to get the code review extension working. See the CodeReview page on the Go Wiki for details.

Log in to the code review site.

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.) The email address you use on the Code Review site will be recorded in the Mercurial change log and in the CONTRIBUTORS file. You can create a Google Account associated with any address where you receive email. If you've enabled the two-step verification feature, don't forget to generate an application-specific password and use that when prompted for a password.

$ 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.

Switch to the default branch

Most Go installations use a release branch, but new changes should only be made to the default branch. (They may be applied later to a release branch as part of the release process.) Before making a change, make sure you use the default branch:

$ hg update default

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.


	<enter description here>


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. Unless explicitly told otherwise, such as in the discussion leading up to sending in the change list, leave the reviewer field blank. This means that the golang-dev@googlegroups.com mailing list will be used as the reviewer.

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, prefixed by the primary affected package, 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: golang-dev@googlegroups.com
CC: math-nuts@swtch.com

	math: improved Sin, Cos and Tan precision for very large arguments.

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


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. (These conventions are described in detail by the Google Project Hosting Issue Tracker documentation.)

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

Adding or removing files from an existing change

If you need to re-edit the change description, or change the files included in the CL, run hg change 99999.

Alternatively, you can use

$ hg file 99999 somefile

to add somefile to CL 99999, and

$ hg file -d 99999 somefile

to remove somefile from the CL.

A file may only belong to a single active CL at a time. hg file will issue a warning if a file is moved between changes.

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:

<<<<<<< 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. First, edit the section to remove the markers and leave the correct code:

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

Then ask Mercurial to mark the conflict as resolved:

$ hg resolve -m flag_test.go

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, but you may still need to run hg resolve -m to mark the conflict resolved.

Mail the change for review

Creating or uploading the change uploads a copy of the diff to the code review server, but it does not notify anyone about it. To do that, you need to run hg mail (see below).

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 golang-dev@googlegroups.com --cc math-nuts@swtch.com 99999

to achieve the same effect.

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

If your change relates to an open issue, please add a comment to the issue announcing your proposed fix, including a link to your CL.

Reviewing code

Running hg mail will send an email to you and the reviewers asking them to visit the issue's URL and make comments 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 done this, you can upload your change to the code review server without sending a notification by running hg upload using the change list number assigned during hg change

$ hg upload 99999

When you have revised the code and are ready for another round of review, run

$ hg mail 99999

again to upload the latest copy and send mail asking the reviewers to please take another look (PTAL). 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.

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.

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

Reviewing code by others

You can import a CL proposed by someone else into your local Mercurial client by using the hg clpatch command. Running

$ hg clpatch 99999

will apply the latest diff for CL 99999 to your working copy. If any of the files referenced in CL 99999 have local modifications, clpatch will refuse to apply the whole diff. Once applied, CL 99999 will show up in the output of hg pending and others.

To revert a CL you have applied locally, use the hg revert command. Running

$ hg revert @99999

will revert any files mentioned on CL 99999 to their original state. This can be an effective way of reverting one CL revision and applying another.

Once the CL has been submitted, the next time you run hg sync it will be removed from your local pending list. Occasionally the pending list can get out of sync leaving stale references to closed or abandoned CLs. You can use hg change -D 99999 to remove the reference to CL 99999.

Submit the change after the review

After the code has been LGTM'ed, it is time to submit it to the Mercurial repository.

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 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.

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 99999
local repository out of date; must sync before submit

Files in the Go repository don't list author names, both to avoid clutter and to avoid having to keep the lists up to date. Instead, your name will appear in the Mercurial change log and in the CONTRIBUTORS file and perhaps the AUTHORS file.

The CONTRIBUTORS file defines who the Go contributors—the people—are; the AUTHORS file defines who “The Go Authors”—the copyright holders—are. The Go developers at Google will update these files when submitting your first change. In order for them to do that, you need to have completed one of the contributor license agreements:

This rigmarole needs to be done only for your first submission.

Code that you contribute should use the standard copyright header:

// Copyright 2013 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.

Files in the repository are copyright the year they are added. It is not necessary to update the copyright year on files that you change.