blob: 76e77c13322804c48efe01a6e37dbcda35a0d70b [file] [log] [blame] [view]
Mark Ayersefa03512017-12-29 13:03:31 -08001## Introduction
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11002
Joshua Wickings285554f2017-10-06 11:38:49 -04003A common use case is to create a reusable library and an application that consumes it, and host both on GitHub. We will illustrate this with a trivial application called "` uselessd `" that consumes a likewise trivial library called "` useless `".
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11004
5
Mark Ayersefa03512017-12-29 13:03:31 -08006## Code Layout
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11007
Joshua Wickings285554f2017-10-06 11:38:49 -04008The app and both libraries live on GitHub, each in its own repository. ` $GOPATH ` is the root of the _project_ - each of your GitHub repos will be checked out several folders below ` $GOPATH `.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11009
10Your code layout would look like this:
11
12```
13$GOPATH/
14 src/
15 github.com/
16 jmcvetta/
17 useless/
18 .git/
19 useless.go
20 useless_test.go
21 README.md
22 uselessd/
23 .git/
24 uselessd.go
25 uselessd_test.go
26 README.md
27```
28
29Each folder under ` src/github.com/jmcvetta/ ` is the root of a separate git checkout.
30
31
Mark Ayersefa03512017-12-29 13:03:31 -080032## $GOPATH
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110033
34Your ` $GOPATH ` variable will point to the root of your Go workspace, as described in [How to Write Go Code](http://golang.org/doc/code.html).
35
36
Mark Ayersefa03512017-12-29 13:03:31 -080037## Note for Eclipse Users
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110038
39Both Go and Eclipse use the term "workspace", but they use it to mean something different. What Go calls a "workspace" is what Eclipse calls a "project". Whenever this document uses either term, it refers to a Go workspace.
40
41
Mark Ayersefa03512017-12-29 13:03:31 -080042### Setup the Workspace
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110043
Joshua Wickings285554f2017-10-06 11:38:49 -040044Let's assume we are starting from scratch. Initialize the two new repositories on GitHub, using the "Initialize this repository with a README" option so your repos can be cloned immediately. Then setup the project like this:
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110045
46```sh
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110047cd ~/workspace # Standard location for Eclipse workspace
48mkdir mygo # Create your Go workspace
49export GOPATH=~/workspace/mygo # GOPATH = Go workspace
50cd $GOPATH
51mkdir -p src/github.com/jmcvetta
52cd src/github.com/jmcvetta
53git clone git@github.com:jmcvetta/useless.git
54git clone git@github.com:jmcvetta/uselessd.git
55```
56
Mark Ayersefa03512017-12-29 13:03:31 -080057## Libraries
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110058
59Conventionally, the name of the repository is the same as the name of the package it contains. Our ` useless ` repo contains ` useless.go ` which defines ` package useless `:
60
61```Go
62package useless
63
64func Foobar() string {
Dave Day0d6986a2014-12-10 15:02:18 +110065 return "Foobar!"
66}
67```
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110068
69
Mark Ayersefa03512017-12-29 13:03:31 -080070## Applications
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110071
72An application - Go code that will be compiled into an executable command - always defines ` package main ` with a ` main() ` function.
73
74So ` uselessd.go ` looks like this:
75
76```Go
77package main
78
79import (
Dave Day0d6986a2014-12-10 15:02:18 +110080 "net/http"
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110081
nathany5f8aa642014-12-10 22:14:43 -080082 "golang.org/x/net/websocket"
Dave Day0d6986a2014-12-10 15:02:18 +110083 "github.com/jmcvetta/useless"
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110084)
85
86func main() {
Dave Day0d6986a2014-12-10 15:02:18 +110087 http.Handle("/useless", websocket.Handler(func(ws *websocket.Conn) {
88 ws.Write([]byte(useless.Foobar()))
89 }))
90 http.ListenAndServe(":3000", nil)
91}
92```
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110093
94
Mark Ayersefa03512017-12-29 13:03:31 -080095## Dependencies
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110096
nathany5f8aa642014-12-10 22:14:43 -080097Your project will probably depend on some existing packages. The application above depends upon ` golang.org/x/net/websocket `. You can install all dependencies by running "` go get -v ./... `" from the root of your workspace. The "` go get `" command is similar to "` go install `" in that it will attempt to build and install all packages in the workspace (technically, all packages matched by "` ./... `"), except that it will also examine their dependencies and download (and install) any that are missing first.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110098
99See the output of "` go help packages `" for a full explanation of the "` ... `" syntax.
100
Soroush Javadid5b54d32018-07-16 16:43:06 +0430101All dependencies will be installed alongside your code under "` $GOPATH/src `". All GitHub repositories checked out by "` go get `" will use the read-only ` https:// ` repository by default. To push changes back to GitHub from one of these repositories, change the "` origin/master `" ref in ` .git/config ` to match the SSH repository from GitHub.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100102
Mark Ayersefa03512017-12-29 13:03:31 -0800103## Build
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100104
Soroush Javadid5b54d32018-07-16 16:43:06 +0430105During development, you can build the ` useless ` library by itself with the command "` go build ...useless `". You could also give the full path to the package name, "` go build github.com/jmcvetta/useless `".
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100106
107To compile ` uselessd.go ` and its dependencies into an executable, use the command "` go build ...uselessd `".
108
Mark Ayersefa03512017-12-29 13:03:31 -0800109## Example Code
Andrew Gerrand5bc444d2014-12-10 11:35:11 +1100110
Eric Browna62ab102017-02-27 22:10:15 -0500111FWIW all the repo addresses on this page are real, and the ` uselessd ` application should compile if you follow the directions above.