github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2012/simple.slide (about) 1 Go: a simple programming environment 2 3 9 Nov 2012 4 5 # Go is a general-purpose language that bridges the gap between efficient 6 # statically typed languages and productive dynamic language. But it’s not just 7 # the language that makes Go special – Go has broad and consistent standard 8 # libraries and powerful but simple tools. 9 # 10 # This talk gives an introduction to Go, followed by a tour of some real 11 # programs that demonstrate the power, scope, and simplicity of the Go 12 # programming environment. 13 14 Andrew Gerrand 15 Google Inc. 16 @enneff 17 adg@golang.org 18 http://golang.org 19 20 * Video 21 22 A video of this talk was recorded at Øredev in Malmö, Sweden in November 2012. 23 24 .link http://vimeo.com/53221558 Watch the talk on Vimeo 25 26 * Background 27 28 * Why a new language? 29 30 Motivated by our needs at Google. 31 32 We need: 33 34 - Efficiency 35 - Safety 36 - Concurrency 37 - Scalability 38 - Fast development cycle 39 - No surprises 40 - A cute mascot 41 42 * Design 43 44 "Consensus drove the design. Nothing went into the language until [Ken Thompson, Robert Griesemer, and myself] all agreed that it was right. Some features didn’t get resolved until after a year or more of discussion." - Rob Pike 45 46 Go is: 47 48 - Lightweight, avoids unnecessary repetition 49 - Object Oriented, but not in the usual way 50 - Concurrent, in a way that keeps you sane 51 - Designed for working programmers 52 53 * Go 1 54 55 Released in March 2012 56 57 A specification of the language and libraries that will be supported for years. 58 59 The guarantee: code written for Go 1.0 will build and run with Go 1.x. 60 61 Best thing we ever did. 62 63 * The gopher 64 65 .image simple/gopher.jpg 66 67 * Hello, go 68 69 .play simple/hello.go 70 71 * Standard library 72 73 * Packages 74 75 Go code lives in packages. 76 77 Packages contain type, function, variable, and constant declarations. 78 79 Packages can be very small (package `errors` has just one declaration) or very large (package `net/http` has >100 declarations). Most are somewhere in between. 80 81 Case determines visibility: `Foo` is exported, `foo` is not 82 83 * io 84 85 The `io` package provides fundamental I/O interfaces that are used throughout most Go code. 86 87 The most ubiquitous are the `Reader` and `Writer` types, which describe streams of data. 88 89 .code simple/io/io.go 90 91 `Reader` and `Writer` implementations include files, sockets, (de)compressors, image and JSON codecs, and many more. 92 93 * Chaining io.Readers 94 95 .play simple/reader.go 96 97 * net/http 98 99 The `net/http` package implements an HTTP server and client. 100 101 .play simple/hello-web.go 102 103 * encoding/json 104 105 The `encoding/json` package converts JSON-encoded data to and from native Go data structures. 106 107 .play simple/json.go /const/,$ 108 109 * time 110 111 The `time` package provides a representation of time and duration, and other time-related functions. 112 113 .play simple/time.go /START/,/END/ 114 .play simple/time2.go /START/,/END/ 115 116 `time.Time` values also contain a `time.Location` (for display only): 117 118 .play simple/time3.go /START/,/END/ 119 120 * flag 121 122 The `flag` package provides a simple API for parsing command-line flags. 123 124 .play simple/flag.go 125 126 $ flag -message 'Hold on...' -delay 5m 127 128 * Tools 129 130 * The go tool 131 132 The `go` tool is the de facto standard for building and installing Go code. 133 134 Compile and run a single-file program: 135 136 $ go run hello.go 137 138 Build and install the package in the current directory (and its dependencies): 139 140 $ go install 141 142 Build and install the `fmt` package (and its dependencies): 143 144 $ go install fmt 145 146 This tool also acts as an interface for most of the Go tools. 147 148 * Import paths 149 150 The `go` tool is a "zero configuration" tool. No Makefiles or scripts. Just Go code. 151 Your build schema and code are always in sync; they are one and the same. 152 153 Package import paths mirror the code's location in the file system: 154 155 src/ 156 github.com/nf/ 157 gosynth/ 158 main.go 159 note.go 160 osc.go 161 wav/ 162 writer.go 163 164 The `gosynth` program imports the `wav` package: 165 166 import "github.com/nf/wav" 167 168 Installing `gosynth` will automatically install the `wav` package: 169 170 $ go install github.com/nf/gosynth 171 172 * Remote dependencies 173 174 The `go` tool also fetches Go code from remote repositories. 175 176 Import paths can be URLs: 177 178 import "golang.org/x/net/websocket" 179 180 To fetch, build and install a package: 181 182 $ go get code.google.com/p/go.net/websocket 183 184 To fetch, build, and install `gosynth` and its dependencies: 185 186 $ go get github.com/nf/gosynth 187 188 This simple design leads to other cool tools: 189 190 .link http://go.pkgdoc.org 191 192 * Godoc 193 194 Godoc extracts documentation from Go code and presents it in a variety of forms. 195 196 Comments need no special format, they just need to precede what they document. 197 198 // Split slices s into all substrings separated by sep and returns a slice of 199 // the substrings between those separators. 200 // If sep is empty, Split splits after each UTF-8 sequence. 201 // It is equivalent to SplitN with a count of -1. 202 func Split(s, sep string) []string { 203 204 .image simple/split.png 205 206 Documentation that lives with code is easy to keep up-to-date. 207 208 * Gofmt 209 210 The `gofmt` tool is a pretty-printer for Go source code. 211 212 All Go code in the core is gofmt'd, as is ~70% of open source Go code. 213 214 Ends boring formatting discussions. 215 216 Improves readability. Improves writability. 217 218 Saves a _huge_ amount of time. 219 220 * Tests: writing 221 222 The `go` tool and the `testing` package provide a lightweight test framework. 223 224 .code simple/test/string_test.go /func TestIndex/,/^}/ 225 226 * Tests: running 227 228 The go tool runs tests. 229 230 $ go test 231 PASS 232 233 $ go test -v 234 === RUN TestIndex 235 --- PASS: TestIndex (0.00 seconds) 236 PASS 237 238 To run the tests for all my projects: 239 240 $ go test github.com/nf/... 241 242 * Tests: benchmarks 243 244 The `testing` package also supports benchmarks. 245 246 A sample benchmark function: 247 248 .code simple/test/string_test.go /func BenchmarkIndex/,/^}/ 249 250 The benchmark package will vary `b.N` until the benchmark function lasts long enough to be timed reliably. 251 252 $ go test -test.bench=Index 253 PASS 254 BenchmarkIndex 50000000 37.3 ns/op 255 256 * Tests: doc examples 257 258 The `testing` package also supports testable examples. 259 260 .code simple/test/string_test.go /func ExampleIndex/,/^}/ 261 262 Examples and built and run as part of the normal test suite: 263 264 $ go test -v 265 === RUN: ExampleIndex 266 --- PASS: ExampleIndex (0.00 seconds) 267 PASS 268 269 The example is displayed in `godoc` alongside the thing it demonstrates: 270 271 .link http://golang.org/pkg/strings/#Index 272 273 * And there's more 274 275 - `vet`: checks code for common programmer mistakes 276 - `pprof`: CPU and memory profiling 277 - `fix`: automatically migrate code as APIs change 278 - GDB support 279 - Editor support: Vim, Emacs, Eclipse, Sublime Text 280 281 * An example 282 283 * Webfront 284 285 `Webfront` is an HTTP server and reverse proxy. 286 287 It reads a JSON-formatted rule file like this: 288 289 .code simple/webfront/main.go /^\[/,/\]/ 290 291 For all requests to the host `example.com` (or any name ending in `".example.com"`) it serves files from the `/var/www` directory. 292 293 For requests to `example.org`, it forwards the request to the HTTP server listening on localhost port 8080. 294 295 * The Rule type 296 297 A `Rule` value specifies what to do for a request to a specific host. 298 299 .code simple/webfront/main.go /Rule represents/,/^}/ 300 301 It corresponds directly with the entries in the JSON configuration file. 302 303 .code simple/webfront/main.go /^\[/,/\]/ 304 305 * Rule methods 306 307 .code simple/webfront/main.go /Match returns/,/^}/ 308 .code simple/webfront/main.go /Handler returns/,/^}/ 309 310 * The Server type 311 312 The `Server` type is responsible for loading (and refreshing) the rules from the rule file and serving HTTP requests with the appropriate handler. 313 314 .code simple/webfront/main.go /Server implements/,/^}/ 315 .code simple/webfront/main.go /ServeHTTP matches/,/^}/ 316 317 * The handler method 318 319 .code simple/webfront/main.go /handler returns/,/^}/ 320 321 * Parsing rules 322 323 The `parseRules` function uses the `encoding/json` package to read the rule file into a Go data structure. 324 325 .code simple/webfront/main.go /parseRules reads/,/^}/ 326 327 * The loadRules method 328 329 .code simple/webfront/main.go /loadRules tests/,/^}/ 330 331 * Constructing the server 332 333 .code simple/webfront/main.go /NewServer constructs/,/^}/ 334 335 This constructor function launches a goroutine running the `refreshRules` method. 336 337 * Refreshing the rules 338 339 .code simple/webfront/main.go /refreshRules polls/,/^}/ 340 341 * Bringing it all together 342 343 The main function parses command-line flags, constructs a `Server`, and launches an HTTP server that serves all requests with the `Server`. 344 345 .code simple/webfront/main.go /^var/,/^}/ 346 347 * Demo 348 349 * Testing (1/3) 350 351 The `Server` integration test uses the `httptest` package to construct a dummy HTTP server, synthesizes a set of rules, and constructs a `Server` instance that uses those rules. 352 353 .code simple/webfront/server_test.go /^func testHandler/,/STOP/ 354 355 * Testing (2/3) 356 357 Each test case in the table specifies a request URL and the expected response code and body. 358 359 .code simple/webfront/server_test.go /TESTS START/,/STOP/ 360 361 * Testing (3/3) 362 363 For each test case, construct an `http.Request` for the url and an `httptest.ResponseRecorder` to capture the response, and pass them to the `Server.ServeHTTP` method. Then check that the response matches the test case. 364 365 .code simple/webfront/server_test.go /RANGE START/,/^}/ 366 367 * Demo 368 369 * Conclusions 370 371 * Further reading 372 373 All about Go: 374 375 .link http://golang.org 376 377 The slides for this talk: 378 379 .link http://talks.golang.org/2012/simple.slide 380 381 webfront: 382 383 .link https://github.com/nf/webfront 384