blob: 228f43b711affad350d100250ee9c587c4f1f796 [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001# Introduction
Bram Geron97ede9e2015-02-03 13:12:04 +00002Now that you've spent many hours writing your package and debugging it and testing it (you did [[test it|TableDrivenTests]], didn't you?), you want to publish it so other people can [go get](http://golang.org/cmd/go/) your package and see it on the [dashboard](https://godoc.org/).
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11003
Kevin Kennell04359a32016-04-05 17:00:00 +02004First, you will need to host it online somewhere. Three major code hosting sites are [bitbucket](http://bitbucket.org/) (hg/git), [GitHub](http://github.com/) (git) and [launchpad](http://launchpad.net) (bzr). I recommend choosing whichever version control system you are familiar with or which your code is versioned locally on your machine. Git (git) is the version control system used by the central Go repository, so it is the closest to a guarantee as you can get that a developer wanting to use your project will have the right software. If you have never used version control before, these websites have some nice HOWTOs and you can find many great tutorials by searching Google for "{name} tutorial" where {name} is the name of the version control system you would like to learn.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11005
6# Package Setup
7## Choosing the Import
nathanye4da9112014-12-10 22:24:41 -08008The full import of your package often has something identifying its author (particularly on hosting sites like GitHub, where "github.com/kylelemons/..." is the full import), should always have the project name, and should end with the name of the package you've developed if it is different from the project name. For instance, the go-gypsy project provides a yaml package, and is written by Kyle Lemons, and thus has the following import path:
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11009```
10import "github.com/kylelemons/go-gypsy/yaml"
11 ^ ^ ^ ^
12 | | | `-- Package name
13 | | `-------- Project name
14 | `------------------- Author's handle
15 `----------------------------- Hosting site
16```
17
Bram Geron97ede9e2015-02-03 13:12:04 +000018Go >= version 1 supports subdirectories of package repositories.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110019
20## Subdirectories
21
22Frequently, the name that you use for your package will include the name "Go" as a prefix, suffix, or part of its acronym, and you may or may not want this to be a part of the actual command or package name in a go source file. Often, you may have both libraries and commands as a part of your package, and these cannot coexist in the same directory. When these things happen, you will want to structure your repository with subdirectories.
23
24For example, consider a project "Go-PublishingExample" that provides a "epub" package and a "publish" command. The directory structure could be:
25```
26./epub/ # Package source, all files package "epub"
27./publish/ # Command source
28./doc/ # Documentation which won't be downloaded
29./examples/ # Example code which won't be downloaded
30```
31
32The import statement for the package would look like:
33```
34import "codesite.tld/authorName/Go-PublishingExample/epub"
35```
36
Bram Geron97ede9e2015-02-03 13:12:04 +000037It is often a good idea to make sure the last directory path (in this case, "epub") matches the name of the package used by the source files in the directory. In this case, no go get-able files were included in the base directory because neither the binary nor the package were to be named "Go-PublishingExample".
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110038
39## Branches and Tags
Bram Geron2f972772015-02-03 13:17:59 +000040
41**Please note that this section is out of date.** The pseudo version numbers below were applicable to Go < version 1; also the Go repository itself uses Git instead of Mercurial now. **Maybe we should remove this section.**
42
Bram Geronf3bbc1b2015-02-03 13:29:01 +000043**You can get up to date information on go get using "go help get" and "go help importpath".**
44
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110045In general, the Go source tree can exist in three basic states. It can be checked out at a Go Release branch (r60 (on Google Code) at the time of this writing -- this is where most users should be), or it can be checked out at a Go Weekly (a new tag for which is made roughly once per week), or at tip (the Mercurial term for the latest change). The last two are primarily for developers of the Go language itself or developers who need features or fixes which have not been introduced into the latest Release.
46
Bram Geron97ede9e2015-02-03 13:12:04 +000047Due to the lielihood that you might continue collaborating on your project with your team on code that is not ready for general consumption, it is recommended that you utilize the tagging or branching functionality of your version control system. The go get tool understands some special tags and branches, which you may want to use to ensure users get a compatible version of your package:
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110048```
49 go.r60 -- A "go.r##" tag will be checked out if the user has that Go release installed
50 go.weekly.2011-07-19 -- A "go.weekly.YYYY-MM-DD" tag will be checked out if the user has that weekly installed
51```
52
Bram Geron97ede9e2015-02-03 13:12:04 +000053Go get will attempt to fall back to the previous tag if the installed one has no matches, and if none are found, will default to installing tip.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110054
55To create and maintain your release tag in mercurial:
56```
57# Create or update a release tag
58hg tag myProj-v0.0 # tag an easy-to-remember version number if you wish
59hg tag go.r60 # tag this as being go release.r60 compatible
60```
61
62To create and maintain a release branch in git:
63```
64# Create a release branch
65git tag myProj-v0.0 # Tag an easy-to-remember version number if you wish
66git checkout -b go.r60 # create a release branch
67git checkout master # to switch back to your master branch
68
69# Update the release branch
70git checkout go.r60 # switch to the release branch
71git merge master # merge in changes from the master branch since last release
72git checkout master # switch back to master branch
73```
74If you are using other branch names, substitute those names where necessary.
75
76It will typically not be necessary to maintain weekly tags or branches, but it can be very useful to maintain the release branch or tag, as this will ensure the widest audience for your project.
77
78## Commands vs Packages
Bram Geron97ede9e2015-02-03 13:12:04 +000079Since go get does not use your project's Makefiles, it is important to understand how it will actually build your project.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110080
Bram Geron97ede9e2015-02-03 13:12:04 +000081All files in the same directory should always share the same package name. Any files named with a ` _test ` or an ` _os ` and/or ` _arch ` suffix will be ignored (unless the os/arch match). If the package name is "main", go get will build an executable from the source files and name it according to the directory name (using the last path segment only). If the package name is anything else, go get will build it as a package and the import path will be the web-accessible URL for your project's root followed by the subdirectory. See [the go get documentation](http://golang.org/cmd/go/#hdr-Download_and_install_packages_and_dependencies) for how to make import paths for code hosting sites other than the main four.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110082
Bram Geron97ede9e2015-02-03 13:12:04 +000083Dependencies between packages in the same project are common. In the case where one package or command in your project depends upon another, you must use the full import path in order for go get to recognize the dependency and make sure it is built. Third-party packages which are imported from source files in your project will also be automatically downloaded and installed by go get if it is not already present.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110084
85To reuse the example above, the file ` ./publish/main.go ` may look something like this:
86```
87package main
88
89import (
Dave Day0d6986a2014-12-10 15:02:18 +110090 "flag"
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110091)
92
93import "codesite.tld/authorName/Go-PublishingExample/epub"
94
95var dir = flag.String("dir", ".", "Directory to publish")
96
97func main() {
Dave Day0d6986a2014-12-10 15:02:18 +110098 flag.Parse()
99 epub.Publish(*dir)
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100100}
101```
102
103A user wishing to install this executable would execute:
104```
Bram Geron97ede9e2015-02-03 13:12:04 +0000105go get codesite.tld/authorName/Go-PublishingExample/publish
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100106```
107which would also install the ` ".../epub" ` package because of the dependency. A developer simply wishing to install the library could execute:
108```
Bram Geron97ede9e2015-02-03 13:12:04 +0000109go get codesite.tld/authorName/Go-PublishingExample/epub
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100110```
Stuart Bellab9e2c92018-07-06 09:32:46 +0100111and (if they had not already installed ` publish `) would only download and install the package. Note that in none of these cases are the examples or documentation downloaded; in most cases these would be available to browse via the code site.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100112
113# Documentation
114## godoc
115When you are preparing to publish a package, you should make sure that the documentation looks correct by running a local copy of godoc. If your package is installed to the go package tree, you can use the following command:
116```
117 godoc -http=:6060 &
118```
119
120Then browse to http://localhost:6060/pkg/ and find your package.
121
122## Dashboard
123The Go Dashboard will use the first line of your package-level comment (also using the normal godoc format) as the "info" text, so make sure this is set. For instance:
124
125```
126// Package epub is an example publishing library.
127package epub
128```
129
130For more information on godoc, see the [Documenting Go Code](http://blog.golang.org/2011/03/godoc-documenting-go-code.html) blog post.