blob: c21b169db89202b1f2702d4ce78905ac9e0cadce [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001# Introduction
2
3A 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 `".
4
5
6# Code Layout
7
8The 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 `.
9
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
32# $GOPATH
33
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
37# Note for Eclipse Users
38
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
42# Setup the Workspace
43
44Let'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:
45
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
57# Libraries
58
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
70# Applications
71
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
95# Dependencies
96
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
101All dependencies will be installed alongside your code under "` $GOPATH/src `". All Github reposities checked out by "` go get `" will be 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.
102
103# Build
104
105During 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 `".
106
107To compile ` uselessd.go ` and its dependencies into an executable, use the command "` go build ...uselessd `".
108
109# Example Code
110
111FWIW all the repo addresses on this page are real, and the ` uselessd ` application should compile if you follow the directions above.