go - university of california, berkeleycs294-101/sp15/slides/go.pdf · several big services are...

49
3/19/15, 7:18 AM Go Page 1 of 49 http://127.0.0.1:3999/2015/res.slide#47 Go Berkeley CS 294-101 Mar 18, 2015 Rob Pike Google

Upload: lekhanh

Post on 30-Jul-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

3/19/15, 7:18 AMGo

Page 1 of 49http://127.0.0.1:3999/2015/res.slide#47

GoBerkeley CS 294-101Mar 18, 2015

Rob PikeGoogle

3/19/15, 7:18 AMGo

Page 2 of 49http://127.0.0.1:3999/2015/res.slide#47

What is Go?

Go is:

designed by Google

open source

concurrent

garbage-collected

compiled

scalable

simple

fun

boring (to some)

golang.org (http://golang.org)

3/19/15, 7:18 AMGo

Page 3 of 49http://127.0.0.1:3999/2015/res.slide#47

Adoption

3/19/15, 7:18 AMGo

Page 4 of 49http://127.0.0.1:3999/2015/res.slide#47

Why?

Go is an answer to problems of scale at Google.

3/19/15, 7:18 AMGo

Page 5 of 49http://127.0.0.1:3999/2015/res.slide#47

How?

By designing a language for software engineering.

3/19/15, 7:18 AMGo

Page 6 of 49http://127.0.0.1:3999/2015/res.slide#47

What is important?

3/19/15, 7:18 AMGo

Page 7 of 49http://127.0.0.1:3999/2015/res.slide#47

Properties

The "abilities":

Readability

Scalability

Suitability

Toolability

3/19/15, 7:18 AMGo

Page 8 of 49http://127.0.0.1:3999/2015/res.slide#47

Readability

3/19/15, 7:18 AMGo

Page 9 of 49http://127.0.0.1:3999/2015/res.slide#47

Overview

The readability of programs is immeasurably more important thantheir writeability.

Hints on Programming Language Design C. A. R. Hoare 1973

3/19/15, 7:18 AMGo

Page 10 of 49http://127.0.0.1:3999/2015/res.slide#47

Readability

The purpose of notation:

clearly express what we care about

3/19/15, 7:18 AMGo

Page 11 of 49http://127.0.0.1:3999/2015/res.slide#47

Clarity: Plan for the future

program for someone else, years from now

one-liners not the gold standard

a balance between clarity and redundancy

3/19/15, 7:18 AMGo

Page 12 of 49http://127.0.0.1:3999/2015/res.slide#47

Too cold

scoped_ptr<goscript::GoScript> goscript(goscript::GoScript::NewGoScript(FLAGS_goscript, goscript::GoScript::kFIFO));

3/19/15, 7:18 AMGo

Page 13 of 49http://127.0.0.1:3999/2015/res.slide#47

Too hot

(n: Int) => (2 to n) |> (r => r.foldLeft(r.toSet)((ps, x) => if (ps(x)) ps -- (x * x to n by x) else ps))

3/19/15, 7:18 AMGo

Page 14 of 49http://127.0.0.1:3999/2015/res.slide#47

Just right

t := time.Now()switch {case t.Hour() < 12: return "morning"case t.Hour() < 18: return "afternoon"default: return "evening"}

3/19/15, 7:18 AMGo

Page 15 of 49http://127.0.0.1:3999/2015/res.slide#47

Naming

How names work in a programming language is critical toreadability.

3/19/15, 7:18 AMGo

Page 16 of 49http://127.0.0.1:3999/2015/res.slide#47

Scope

Go has very simple scope hierarchy:

universe

package

file (for imports only)

function

block

3/19/15, 7:18 AMGo

Page 17 of 49http://127.0.0.1:3999/2015/res.slide#47

Locality of names

Nuances:

upper case names for visibility: name vs. Name

no implicit this in methods (receiver is explicit); always seercvr.Field

package qualifier always present for imported names

(first component of) every name is always declared incurrent package

3/19/15, 7:18 AMGo

Page 18 of 49http://127.0.0.1:3999/2015/res.slide#47

Locality scales

No surprises when importing:

adding an exported name to my package cannot breakyour package!

Names do not leak across boundaries.

In C, C++, Java the name y could refer to anything. In Go, y (or even Y) is always defined within the package. In Go, x.Y is clear: find x locally, Y belongs to it.

3/19/15, 7:18 AMGo

Page 19 of 49http://127.0.0.1:3999/2015/res.slide#47

Function and method lookup

Method lookup by name only, not type. A type cannot have two methods with the same name, ever. Easy to identify which function/method is referred to. Simple implementation, simpler program, fewer surprises.

Given a method x.M, there's only ever one M associated with x.

3/19/15, 7:18 AMGo

Page 20 of 49http://127.0.0.1:3999/2015/res.slide#47

Scalability

3/19/15, 7:18 AMGo

Page 21 of 49http://127.0.0.1:3999/2015/res.slide#47

3/19/15, 7:18 AMGo

Page 22 of 49http://127.0.0.1:3999/2015/res.slide#47

3/19/15, 7:18 AMGo

Page 23 of 49http://127.0.0.1:3999/2015/res.slide#47

3/19/15, 7:18 AMGo

Page 24 of 49http://127.0.0.1:3999/2015/res.slide#47

3/19/15, 7:18 AMGo

Page 25 of 49http://127.0.0.1:3999/2015/res.slide#47

3/19/15, 7:18 AMGo

Page 26 of 49http://127.0.0.1:3999/2015/res.slide#47

Scalability

Google means scale in multiple dimensions

computers

cores

data

code

engineers

Plus scaling has a big effect on:

speed of compilation

speed of testing

3/19/15, 7:18 AMGo

Page 27 of 49http://127.0.0.1:3999/2015/res.slide#47

System scale

3/19/15, 7:18 AMGo

Page 28 of 49http://127.0.0.1:3999/2015/res.slide#47

System scale

10⁶⁺ machines (design point)

routine to be running on 1000 machines

coordinating, interacting with other servers

lots going on at once

Solution: great support for concurrency

3/19/15, 7:18 AMGo

Page 29 of 49http://127.0.0.1:3999/2015/res.slide#47

Engineering scale

3/19/15, 7:18 AMGo

Page 30 of 49http://127.0.0.1:3999/2015/res.slide#47

Engineering scale

In 2011 at Google:

single code tree

5000+ developers across 40+ offices

20+ changes per minute

50% of source files change every month

50 million test cases executed per day

Solution: engineer language for large code bases

3/19/15, 7:18 AMGo

Page 31 of 49http://127.0.0.1:3999/2015/res.slide#47

Software scale

3/19/15, 7:18 AMGo

Page 32 of 49http://127.0.0.1:3999/2015/res.slide#47

Dependencies in C++

Explosive, exponential, almost non-computable.

In 2007, instrumented building a large Google web-servingbinary:

2000 files

4.2 megabytes

8 gigabytes delivered to compiler

2000 bytes sent to compiler for every C++ source byte

it's real work too: <string> for example

hours to build

3/19/15, 7:18 AMGo

Page 33 of 49http://127.0.0.1:3999/2015/res.slide#47

Dependencies in Go

Linguistically defined.

Efficient.

Computable.

3/19/15, 7:18 AMGo

Page 34 of 49http://127.0.0.1:3999/2015/res.slide#47

Hoisting dependencies

Consider: A imports B imports C but A does not directly import C.

The object code for B includes all the information about Cneeded to import B. Therefore in A the line

import "B"

does not require the compiler to read C when compiling A.

Also, the object files are designed so the "export" informationcomes first; compiler doing import does not need to readwhole file.

Exponentially less data read than with #include files.

With Go in Google, about 40X fanout (recall C++ was 2000x) Plus in C++ it's general code that must be parsed; in Go it's justexport data.

3/19/15, 7:18 AMGo

Page 35 of 49http://127.0.0.1:3999/2015/res.slide#47

Scalability requires readability

For code to grow safely as time passes and staff changes:

it must be readable

it must be clear

it must be adaptable

it must be local

The themes resonate.

3/19/15, 7:18 AMGo

Page 36 of 49http://127.0.0.1:3999/2015/res.slide#47

Suitability

3/19/15, 7:18 AMGo

Page 37 of 49http://127.0.0.1:3999/2015/res.slide#47

Suitability

Can the language do the job?

Language is notation for a problem; not all languages are goodfor all problems.

Go was designed for Google to help solve Google's problems.

3/19/15, 7:18 AMGo

Page 38 of 49http://127.0.0.1:3999/2015/res.slide#47

Concurrency is vital

Linguistic support for concurrent execution makesprogramming in the Google environment easier, safer, andmore productive.

A key reason for Go's existence.

3/19/15, 7:18 AMGo

Page 39 of 49http://127.0.0.1:3999/2015/res.slide#47

Go in production

Several big services are written in Go:

golang.org

dl.google.com

vitess, part of youtube.com

...

Adoption finds issues; they are resolved; adoption easier nexttime.

3/19/15, 7:18 AMGo

Page 40 of 49http://127.0.0.1:3999/2015/res.slide#47

SPDY

SPDY proxy for Chrome on mobile devices

3/19/15, 7:18 AMGo

Page 41 of 49http://127.0.0.1:3999/2015/res.slide#47

Toolability

3/19/15, 7:18 AMGo

Page 42 of 49http://127.0.0.1:3999/2015/res.slide#47

Toolability

Software engineering requires tools.

Go's syntax, package design, naming, etc. make tools easy towrite.

Library includes lexer, parser and type checker.

3/19/15, 7:18 AMGo

Page 43 of 49http://127.0.0.1:3999/2015/res.slide#47

Gofmt

Always intended to do automatic code formatting. Eliminates an entire class of argument. Runs as a "presubmit" to the code repositories.

Training:

The community has always seen gofmt output.

Sharing:

Uniformity of presentation simplifies sharing.

Scaling:

Less time spent on formatting, more on content.

Often cited as one of Go's best features.

3/19/15, 7:18 AMGo

Page 44 of 49http://127.0.0.1:3999/2015/res.slide#47

Gofmt and other tools

Surprise: The existence of gofmt enabled semantic tools: Can rewrite the tree; gofmt will clean up output.

Examples:

gofmt -r 'a[b:len(a)] -> a[b:]'

gofix

And good front-end libraries enable ancillary tools:

godoc

go get, go build, go vet, etc.

api

3/19/15, 7:18 AMGo

Page 45 of 49http://127.0.0.1:3999/2015/res.slide#47

Gofix

The gofix tool allowed us to make sweeping changes to APIsand language features leading up to the release of Go 1.

changed syntax for deleting from a map

new time API

many more

Also allows us to update code even if the old code still works.

More recent example:

Changed Go's protocol buffer implementation to use getterfunctions; used gofix to update all google3 Go code.

3/19/15, 7:18 AMGo

Page 46 of 49http://127.0.0.1:3999/2015/res.slide#47

Conclusion

Clarity is key.

Design for readability, not writeability.

Readability creates clarity, improving:

productivity

scale

tooling

These effects multiply.

3/19/15, 7:18 AMGo

Page 47 of 49http://127.0.0.1:3999/2015/res.slide#47

Questions?

Links:

golang.org (http://golang.org)

talks.golang.org/2012/splash.article (http://talks.golang.org/2012/splash.article)

3/19/15, 7:18 AMGo

Page 48 of 49http://127.0.0.1:3999/2015/res.slide#47

Thank you

Berkeley CS 294-101Mar 18, 2015

Rob [email protected] (mailto:[email protected])

http://golang.org/ (http://golang.org/)

3/19/15, 7:18 AMGo

Page 49 of 49http://127.0.0.1:3999/2015/res.slide#47