blob: 6eb8dee7a7c213e6be024f0967b8eb415f7feec1 [file] [log] [blame]
Go for Java Programmers
Sameer Ajmani
Tech Lead Manager, Go team
Google
@Sajma
sameer@golang.org
* Video
This talk was presented at [[http://javasig.com][NYJavaSIG]] on April 23, 2015.
.link https://www.youtube.com/watch?v=_c_tQ6_3cCg Watch the talk on YouTube
* Outline
1. What is Go, and who uses it?
2. Comparing Go and Java
3. Examples
4. Concurrency
5. Tools
# The next several slides are from rsc's 2013/distsys and 2015/mit talks.
* What is Go?
"Go is an open source programming language that makes it easy to build simple, reliable, and efficient software."
.link http://golang.org
* History
Design began in late 2007.
- Robert Griesemer, Rob Pike, and Ken Thompson.
- Ian Lance Taylor and Russ Cox.
Open source since 2009 with a very active community.
Language stable as of Go 1, early 2012.
* Why Go?
Go is an answer to problems of scale at Google.
.image ../2012/splash/datacenter.jpg 500 _
* System Scale
- designed to scale to 10⁶⁺ machines
- everyday jobs run on 1000s of machines
- jobs coordinate, interact with others in the system
- lots going on at once
Solution: great support for concurrency
.image ../2012/waza/gophercomplex6.jpg
* A Second Problem: Engineering Scale
In 2011:
- 5000+ developers across 40+ offices
- 20+ changes per minute
- 50% of code base changes every month
- 50 million test cases executed per day
- single code tree
Solution: design the language for large code bases
* Who uses Go at Google?
Lots of projects. Thousands of Go programmers. Millions of lines of Go code.
Public examples:
- SPDY proxy for Chrome on mobile devices
.image go-for-java-programmers/spdy.png 400 _
* Who uses Go at Google?
Lots of projects. Thousands of Go programmers. Millions of lines of Go code.
Public examples:
- SPDY proxy for Chrome on mobile devices
- Download server for Chrome, ChromeOS, Android SDK, Earth, etc.
- YouTube Vitess MySQL balancer
The target is networked servers, but it's a great general-purpose language.
* Who uses Go besides Google?
.link http://golang.org/wiki/GoUsers
Apcera, Bitbucket, bitly, Canonical, CloudFlare, Core OS, Digital Ocean, Docker, Dropbox, Facebook, Getty Images, GitHub, Heroku, Iron.io, Kubernetes, Medium, MongoDB services, Mozilla services, New York Times, pool.ntp.org, Secret, SmugMug, SoundCloud, Stripe, Square, Thomson Reuters, Tumblr, ...
.image ../2014/state-of-go/bus.jpg 300 _
* Comparing Go and Java
* Go and Java have much in common
- C family (imperative, braces)
- Statically typed
- Garbage collected
- Memory safe (nil references, runtime bounds checks)
- Variables are always initialized (zero/nil/false)
- Methods
- Interfaces
- Type assertions (`instanceof`)
- Reflection
* Go differs from Java in several ways
- Programs compile to machine code. There's no VM.
- Statically linked binaries
- Control over memory layout
- Function values and lexical closures
- Built-in strings (UTF-8)
- Built-in generic maps and arrays/slices
- Built-in concurrency
* Go intentionally leaves out many features
- No classes
- No constructors
- No inheritance
- No `final`
- No exceptions
- No annotations
- No user-defined generics
* Why does Go leave out those features?
Clarity is critical.
When reading code, it should be clear what the program will do.
When writing code, it should be clear how to make the program do what you want.
Sometimes this means writing out a loop instead of invoking an obscure function.
(Don't DRY out.)
For more background on design:
- [[http://commandcenter.blogspot.com/2012/06/less-is-exponentially-more.html][Less is exponentially more (Pike, 2012)]]
- [[http://go.dev/talks/2012/splash.article][Go at Google: Language Design in the Service of Software Engineering (Pike, 2012)]]
* Examples
* Go looks familiar to Java programmers
Main.java
.code go-for-java-programmers/hello/Main.java
hello.go
.play go-for-java-programmers/hello/hello.go
* Hello, web server
.play go-for-java-programmers/hello/server.go
Types follow names in declarations.
Exported names are Capitalized. Unexported names are not.
* Example: Google Search frontend
.image go-for-java-programmers/frontend-screenshot.png _ 1000
.play go-for-java-programmers/frontend.go /func main/,/func handleSearch/
* Validate the query
.code go-for-java-programmers/frontend.go /func handleSearch/,/ENDQUERY/
`FormValue` is a method on the type `*http.Request`:
package http
type Request struct {...}
func (r *Request) FormValue(key string) string {...}
`query`:=`req.FormValue("q")` initializes a new variable `query` with
the type of the expression on the right hand side, `string`.
* Fetch the search results
.code go-for-java-programmers/frontend.go /Run the Google search/,/ENDSEARCH/
`Search` returns two values, a slice of results and an error.
func Search(query string) ([]Result, error) {...}
The results are valid only if the error is nil.
type error interface {
Error() string // a useful human-readable error message
}
Errors may contain additional information, accessed via type assertions.
* Render the search results
.code go-for-java-programmers/frontend.go /Render the/,/ENDRENDER/
`resultsTemplate.Execute` generates HTML and writes it to an `io.Writer`:
type Writer interface {
Write(p []byte) (n int, err error)
}
`http.ResponseWriter` implements the `io.Writer` interface.
* HTML templates operate on Go values
.play go-for-java-programmers/frontend.go /A Result contains/,/\)\)/
* Issue the query to the Google Search API
.code go-for-java-programmers/frontend.go /func Search/,/resp.Body.Close/
The `defer` statement arranges for `resp.Body.Close` to run when `Search` returns.
* Parse the JSON response into a Go struct
.link https://developers.google.com/web-search/docs/#fonje
.code go-for-java-programmers/frontend.go /var jsonResponse/,/^}/
* That's it for the frontend
All the packages are from the standard library:
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"time"
)
Go servers scale well: each request runs in its own _goroutine_.
Let's talk about concurrency.
* Communicating Sequential Processes (Hoare, 1978)
Concurrent programs are structured as independent processes that
execute sequentially and communicate by passing messages.
Sequential execution is easy to understand. Async callbacks are not.
"Don't communicate by sharing memory, share memory by communicating."
*Go*primitives:* goroutines, channels, and the `select` statement.
* Goroutines
Goroutines are like lightweight threads.
They start with tiny stacks and resize as needed.
Go programs can have hundreds of thousands of them.
Start a goroutine using the `go` statement:
go f(args)
The Go runtime schedules goroutines onto OS threads.
Blocked goroutines don't use a thread.
* Channels
Channels provide communication between goroutines.
c := make(chan string)
// goroutine 1
c <- "hello!"
// goroutine 2
s := <-c
fmt.Println(s) // "hello!"
* Select
A `select` statement blocks until communication can proceed.
select {
case n := <-in:
fmt.Println("received", n)
case out <- v:
fmt.Println("sent", v)
}
Only the selected case runs.
* Example: Google Search (backend)
Q: What does Google search do?
A: Given a query, return a page of search results (and some ads).
Q: How do we get the search results?
A: Send the query to Web search, Image search, YouTube, Maps, News, etc., then mix the results.
How do we implement this?
* Google Search: A fake framework
We can simulate a Search function with a random timeout up to 100ms.
.code go-for-java-programmers/google-serial.go /START2/,/STOP2/
* Google Search: Test the framework
.play go-for-java-programmers/google-serial.go /func.main/,/}/
* Google Search (serial)
The Google function takes a query and returns a slice of Results (which are just strings).
Google invokes Web, Image, and Video searches serially, appending them to the results slice.
.play go-for-java-programmers/google-serial.go /START1/,/STOP1/
* Google Search (parallel)
Run the Web, Image, and Video searches concurrently, and wait for all results.
The `func` literals are closures over `query` and `c`.
.play go-for-java-programmers/google-parallel.go /Google/,/^}/
* Google Search (timeout)
Don't wait for slow servers.
No locks. No condition variables. No callbacks.
.play go-for-java-programmers/google-timeout.go /START/,/STOP/
* Avoid timeout
Q: How do we avoid discarding results from slow servers?
A: Replicate the servers. Send requests to multiple replicas, and use the first response.
.code go-for-java-programmers/first.go /START1/,/STOP1/
* Using the First function
.play go-for-java-programmers/first.go /START2/,/STOP2/
* Google Search (replicated)
Reduce tail latency using replicated search servers.
.play go-for-java-programmers/google-first.go /START/,/STOP/
* And still…
No locks. No condition variables. No callbacks.
* Summary
In just a few simple transformations we used Go's concurrency primitives to convert a
- slow
- sequential
- failure-sensitive
program into one that is
- fast
- concurrent
- replicated
- robust.
* Tools
* Go has great tools
- gofmt and goimports
- the go tool
- godoc
- IDE and editor support
The language is designed for tooling.
* gofmt and goimports
Gofmt formats code automatically. No options.
Goimports updates import statements based on your workspace.
Most people run these tools on save.
.link http://play.golang.org/p/GPqra77cBK
* The go tool
The go tool builds Go programs from source in a conventional directory layout.
No Makefiles or other configs.
Fetch the `present` tool and its dependencies, build it, and install it:
% go get golang.org/x/tools/cmd/present
Run it:
% present
* godoc
Generated documentation for the world's open-source Go code:
.link http://godoc.org
* IDE and editor support
Eclipse, IntelliJ, emacs, vim, many others.
- `gofmt`
- `goimports`
- `godoc` lookups
- code completion
- code navigation
There's no "Go IDE".
Go tools meet you where you are.
* Where to Go next
Take the Go Tour online.
.link http://tour.golang.org
Lots more material.
.link http://golang.org/wiki/Learn
Great community.
.link http://golang.org/project