github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2012/10things.slide (about) 1 10 things you (probably) don't know about Go 2 3 Andrew Gerrand 4 Gopher 5 https://plus.google.com/106356964679457436995 6 @enneff 7 http://golang.org 8 9 * 1. Anonymous structs 10 11 - Grouped globals 12 13 var config struct { 14 APIKey string 15 OAuthConfig oauth.Config 16 } 17 18 config.APIKey = "BADC0C0A" 19 20 - Template data 21 22 data := struct { 23 Title string 24 Users []*User 25 }{ 26 title, 27 users, 28 } 29 err := tmpl.Execute(w, data) 30 31 (Cheaper and safer than using `map[string]interface{}`.) 32 33 * 1b. Anonymous structs 34 35 - Test tables 36 37 var indexRuneTests = []struct { 38 s string 39 rune rune 40 out int 41 }{ 42 {"a A x", 'A', 2}, 43 {"some_text=some_value", '=', 9}, 44 {"☺a", 'a', 3}, 45 {"a☻☺b", '☺', 4}, 46 } 47 48 - Embedded lock 49 50 var hits struct { 51 sync.Mutex 52 n int 53 } 54 55 hits.Lock() 56 hits.n++ 57 hits.Unlock() 58 59 60 * 2. Nested structs 61 62 - Decoding deeply nested JSON data 63 64 {"data": {"children": [ 65 {"data": { 66 "title": "The Go homepage", 67 "url": "http://golang.org/" 68 }}, 69 ... 70 ]}} 71 72 type Item struct { 73 Title string 74 URL string 75 } 76 77 type Response struct { 78 Data struct { 79 Children []struct { 80 Data Item 81 } 82 } 83 } 84 85 86 * 3. Command-line godoc 87 88 % godoc sync Mutex 89 PACKAGE 90 91 package sync 92 import "sync" 93 94 TYPES 95 96 type Mutex struct { 97 // contains filtered or unexported fields 98 } 99 A Mutex is a mutual exclusion lock. Mutexes can be created as part of 100 other structures; the zero value for a Mutex is an unlocked mutex. 101 102 func (m *Mutex) Lock() 103 Lock locks m. If the lock is already in use, the calling goroutine 104 blocks until the mutex is available. 105 106 func (m *Mutex) Unlock() 107 Unlock unlocks m. It is a run-time error if m is not locked on entry to 108 Unlock. 109 110 A locked Mutex is not associated with a particular goroutine. It is 111 allowed for one goroutine to lock a Mutex and then arrange for another 112 goroutine to unlock it. 113 114 115 * 4. godoc -src 116 117 % godoc -src sync Mutex 118 // A Mutex is a mutual exclusion lock. 119 // Mutexes can be created as part of other structures; 120 // the zero value for a Mutex is an unlocked mutex. 121 type Mutex struct { 122 state int32 123 sema uint32 124 } 125 126 Also shows unexported state! Great for digging around. 127 128 129 * 5. go get supports custom domains 130 131 Yep: 132 133 go get camlistore.org/pkg/netutil 134 135 See `go help importpath` for the details. 136 137 138 * 6. Mock out the file system 139 140 Got a package that works with the file system, but don't want your tests to actually use the disk? 141 142 var fs fileSystem = osFS{} 143 144 type fileSystem interface { 145 Open(name string) (file, error) 146 Stat(name string) (os.FileInfo, error) 147 } 148 149 type file interface { 150 io.Closer 151 io.Reader 152 io.ReaderAt 153 io.Seeker 154 Stat() (os.FileInfo, error) 155 } 156 157 // osFS implements fileSystem using the local disk. 158 type osFS struct{} 159 160 func (osFS) Open(name string) (file, error) { return os.Open(name) } 161 func (osFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) } 162 163 Implement your own fake `fileSystem` in and put it in `fs` while testing. 164 165 166 * 7. Method expressions 167 168 type T struct {} 169 func (T) Foo(s string) { println(s) } 170 171 var fn func(T, string) = T.Foo 172 173 Real example from `os/exec`: 174 175 func (c *Cmd) stdin() (f *os.File, err error) 176 func (c *Cmd) stdout() (f *os.File, err error) 177 func (c *Cmd) stderr() (f *os.File, err error) 178 179 type F func(*Cmd) (*os.File, error) 180 for _, setupFd := range []F{(*Cmd).stdin, (*Cmd).stdout, (*Cmd).stderr} { 181 fd, err := setupFd(c) 182 if err != nil { 183 c.closeDescriptors(c.closeAfterStart) 184 c.closeDescriptors(c.closeAfterWait) 185 return err 186 } 187 c.childFiles = append(c.childFiles, fd) 188 } 189 190 191 * 8. Send and receive on the same channel 192 193 .play 10things/8.go 194 195 196 * 9. Using close to broadcast 197 198 .play 10things/9.go /func.waiter/,/endmain/ 199 200 201 * 9b. Using close to broadcast 202 203 .play 10things/9b.go /func.worker/,/endmain/ 204 205 206 * 10. Nil channel in select 207 208 .play 10things/10.go /func.worker/,/endmain/ 209 210 211 * 11. The gopher's name 212 213 .image 10things/gopher.jpg 214