Go » History » Version 7
Misha Zatsman, 12/13/2014 01:46 AM
1 | 1 | Misha Zatsman | h1. Go |
---|---|---|---|
2 | |||
3 | {{toc}} |
||
4 | |||
5 | h2. Using Go with Arvados |
||
6 | |||
7 | h3. Install Go |
||
8 | |||
9 | If you're installing Go on your linux machine you'll want to follow the "tarball instructions":http://golang.org/doc/install#tarball to get the latest stable version, because if you use <code>apt-get</code> you'll get an outdated version. |
||
10 | |||
11 | The first time you install Go, you'll need to set @GOPATH@ to an empty directory. The Go toolchain will install Go packages and dependencies here. |
||
12 | |||
13 | <pre> |
||
14 | export GOPATH=~/gocode |
||
15 | mkdir -p $GOPATH |
||
16 | </pre> |
||
17 | |||
18 | The rest of these instructions assume that you have a working Go installation on your system, and that your @GOPATH@ environment variable is set appropriately. |
||
19 | |||
20 | |||
21 | h3. Install Arvados source |
||
22 | |||
23 | Clone the Arvados git repository, if you have not already: |
||
24 | |||
25 | <pre> |
||
26 | cd |
||
27 | git clone git://git.curoverse.com/arvados.git |
||
28 | </pre> |
||
29 | |||
30 | *Note:* If you are an authorized committer, clone @git@git.curoverse.com:arvados.git@ instead so you may push directly to git.curoverse.com. |
||
31 | |||
32 | h3. Tell Go to use your local Arvados source |
||
33 | |||
34 | This step ensures that your development environment uses your locally-modified code, instead of fetching the master branch from git.curoverse.com: |
||
35 | |||
36 | <pre> |
||
37 | mkdir -p $GOPATH/src/git.curoverse.com |
||
38 | ln -s ~/arvados $GOPATH/src/git.curoverse.com/arvados.git |
||
39 | </pre> |
||
40 | |||
41 | Reason: The Keepstore and Keepproxy packages import other Go packages from the Arvados source tree. These packages have names like: |
||
42 | |||
43 | <pre> |
||
44 | git.curoverse.com/arvados.git/sdk/go/keepclient |
||
45 | git.curoverse.com/arvados.git/sdk/go/arvadosclient |
||
46 | </pre> |
||
47 | |||
48 | When the Go compiler needs to import one of these packages, it will look in @$GOPATH/src@ for the package source code. If it does not find the code locally, it will fetch the code from git.curoverse.com automatically. This symlink ensures that Go will find your local source code under @$GOPATH/src/git.curoverse.com/arvados.git/...@ |
||
49 | |||
50 | h3. Run some tests |
||
51 | |||
52 | e.g. |
||
53 | |||
54 | <pre> |
||
55 | cd ~/arvados/services/keepstore |
||
56 | go test |
||
57 | </pre> |
||
58 | |||
59 | The @go test@ command will print a few dozen lines of logging output. If the tests succeeded, it will print PASS followed by a summary of the packages which passed testing, e.g.: |
||
60 | |||
61 | <pre> |
||
62 | PASS |
||
63 | ok _/home/you/arvados/services/keepstore 1.023s |
||
64 | </pre> |
||
65 | |||
66 | 4 | Misha Zatsman | h2. Arvados Go SDK |
67 | |||
68 | h3. Documentation |
||
69 | |||
70 | For more information consult the "Arvados Go SDK documentation":http://doc.arvados.org/sdk/go/ |
||
71 | |||
72 | h3. Recipes |
||
73 | |||
74 | 5 | Misha Zatsman | This section contains code demonstrating aspects of the Arvados Go SDK. Additional examples can be found in the "Arvados Go SDK documentation":http://doc.arvados.org/sdk/go/ |
75 | 4 | Misha Zatsman | |
76 | h4. Collections filtered and ordered |
||
77 | |||
78 | 6 | Misha Zatsman | This example prints collections last modified after a given date, grouped by owner (owner UUID sorted DESCending) and then ordered by oldest modifications (i.e. ASCending modification date). |
79 | 1 | Misha Zatsman | |
80 | 7 | Misha Zatsman | _Although not an issue in this example, it's worth noting that Arvados only stores the latest modification date for each collection. Therefore if you request collections modified before a given date, the SDK will not return collections who had also been modified after that date._ |
81 | 6 | Misha Zatsman | |
82 | 4 | Misha Zatsman | <pre> |
83 | package main |
||
84 | |||
85 | // ******************* |
||
86 | // Import the modules. |
||
87 | |||
88 | import ( |
||
89 | "git.curoverse.com/arvados.git/sdk/go/arvadosclient" |
||
90 | "log" |
||
91 | ) |
||
92 | |||
93 | type SdkCollectionInfo struct { |
||
94 | Uuid string `json:"uuid"` |
||
95 | OwnerUuid string `json:"owner_uuid"` |
||
96 | Redundancy int `json:"redundancy"` |
||
97 | ModifiedAt string `json:"modified_at"` |
||
98 | ManifestText string `json:"manifest_text"` |
||
99 | } |
||
100 | |||
101 | type SdkCollectionList struct { |
||
102 | ItemsAvailable int `json:"items_available"` |
||
103 | Items []SdkCollectionInfo `json:"items"` |
||
104 | } |
||
105 | |||
106 | func main() { |
||
107 | // ******************************** |
||
108 | // Set up an API client user agent. |
||
109 | // |
||
110 | |||
111 | arv, err := arvadosclient.MakeArvadosClient() |
||
112 | if err != nil { |
||
113 | log.Fatalf("Error setting up arvados client %s", err.Error()) |
||
114 | } |
||
115 | |||
116 | GetCollections(arv) |
||
117 | } |
||
118 | |||
119 | func GetCollections(arv arvadosclient.ArvadosClient) () { |
||
120 | fieldsWanted := []string{"manifest_text", |
||
121 | "owner_uuid", |
||
122 | "uuid", |
||
123 | "redundancy", |
||
124 | "modified_at"} |
||
125 | 1 | Misha Zatsman | |
126 | sdkParams := arvadosclient.Dict{ |
||
127 | 4 | Misha Zatsman | "select": fieldsWanted, |
128 | 6 | Misha Zatsman | "order": []string{"owner_uuid DESC", "modified_at ASC"}, |
129 | "filters": [][]string{[]string{"modified_at", ">=", "2014-01-03T06:53:08Z"}}} |
||
130 | 4 | Misha Zatsman | |
131 | var collections SdkCollectionList |
||
132 | |||
133 | err := arv.List("collections", sdkParams, &collections) |
||
134 | if err != nil { |
||
135 | log.Fatalf("error querying collections: %v", err) |
||
136 | } |
||
137 | |||
138 | for _, collection := range collections.Items { |
||
139 | log.Printf("Seeing owner uuid, modification date: %s %s", |
||
140 | collection.OwnerUuid, |
||
141 | collection.ModifiedAt) |
||
142 | } |
||
143 | return |
||
144 | } |
||
145 | </pre> |
||
146 | 3 | Misha Zatsman | |
147 | 1 | Misha Zatsman | h2. Learning Go |
148 | |||
149 | Getting good at Go, and concurrency/goroutines in particular, is an excellent use of time. |
||
150 | |||
151 | h3. Introductions |
||
152 | |||
153 | * "Go Tour":http://tour.golang.org/ is a great way to spend a few hours getting your feet wet. |
||
154 | * "Effective Go":http://golang.org/doc/effective_go.html The web site says that you should do both the tour and the language specification first before reading this, but honestly, I think it's ridiculous to try to read the language specification before you even start writing code in the language. I wouldn't try to read either the specification or "Effective Go" in depth before trying to write any code, but they're both at least worth skimming at this point. |
||
155 | * The slides for Rob Pike's talk on "Go Concurrency Patterns":http://talks.golang.org/2012/concurrency.slide#1. I haven't watched all of the talks yet; these slides are really nice because they show very elegant ways of using channels and goroutines to build rich and complex concurrent abstractions. |
||
156 | * "The package documentation.":http://golang.org/pkg/ Note that the package docs include links to web versions of the package source code, which help very much in learning idiomatic Go patterns. |
||
157 | |||
158 | h3. Resources |
||
159 | |||
160 | h4. Free online resources: |
||
161 | |||
162 | * http://golang.org |
||
163 | * http://golang.org/doc/code.html |
||
164 | * http://tour.golang.org/ |
||
165 | * https://gobyexample.com/ |
||
166 | * http://learnxinyminutes.com/docs/go/ |
||
167 | |||
168 | h4. Books |
||
169 | |||
170 | * http://www.golang-book.com/ (Free!) |
||
171 | * "Programming in Go":http://www.amazon.com/Programming-Go-Creating-Applications-Developers/dp/0321774639/ref=pd_bxgy_b_img_y (a bit dated now) |