Movatterモバイル変換


[0]ホーム

URL:


Go: 90% Perfect, 100% of the time.

"GoCon Tokyo"

31 May 2014

Brad Fitzpatrick

Hello!

2

"60% of the time, it works every time...."

3

"That makes no sense"

4

Starting a new project, picking a language...

5

Disclaimer

6

2010

7

Idea: Camlistore

8

Which language to use for Camlistore?

9

Before Go

10

Perl: The Good

11

Python, Ruby, #"gocon-tokyo/funfast-nogo.svg">16

Maybe mix of two languages?

  • Fun, slow parts in Perlythonubyscript?
  • Important, fast parts in C?
17

But...

  • You'll want to share code.
  • Calling from C to X or X to C is slow.
  • The C-X glue language is the least fun language
  • You'll be lazy, and write in the fun language
  • Or be lazy and waste time, and write too much stuff in C++ (like Google)
18

Server? Threads or events?

19

Threads

  • C, C++, Java, ...
  • Threads? Heavy. Lots of memory per thread for stacks. Be careful!Executor pool = new ScheduledThreadPoolExecutor(/* ???? guess a number */);
20

Events

  • Perlythonubyscript: events. no choice. Callback hell.
  • C, C++: libevent, etc. Callback hell.
  • C#: await async await async await async await async noise
21

Concurrency and performance

22

It's really hard to pick a language!

23

2010. Let's try Go!

24

Go: 90% Perfect, 100% of the time.

25

Go: 90% Perfect, 100% of the time.

  • Go is very good at lots of things
  • High-level code when you want
  • Low-level code when you want
  • Start high, profile, go low-level when needed
  • Static typing without too much keyboard typing
26

Before Go

27

After Go

28

Go's Concurrency

  • Built-in! Lightweight threads, no callback hell
  • Readable, top-down code
  • So easy to write servers
for {  conn, err := listener.Accept()  // check err  go serve(conn)}
  • Goroutine-per-request can scale
29

Concurrency before Go

30

After Go

31

Go is showing up for all sorts of tasks

32

Web frameworks

  • Typically a job for scripting languages: Node.js, Python, Ruby, Perl, PHP, etc.
  • net/http in the standard library
  • Other http frameworks on top: gorilla, martini, Revel etc...
  • No need for nginx: standard library's http server scales
33

Image processing

  • no libpng, imagemagick, etc
  • pure Go PNG, JPEG, GIF encoding & decoding in the standard library
  • slower, but safer
  • getting faster
34

Crypto

  • no OpenSSL, no GnuTLS
  • pure Go AES, TLS (SSL), OpenPGP, etc
  • slower, but safer
  • getting faster
35

Replacing shell scripts

  • Instead of little Perl & shell scripts, I now write in Go
  • os/exec package & goroutines makes subprocess management easier than shell
36

Camlistore

  • web handlers
  • using third-party web APIs
  • image thumbnails: decode, resize, encode
  • EXIF, ID3 parsing
  • database drivers
  • crypto, image decoding/resizing/encoding
  • search, indexing: tight memory layout control
  • WebSockets, HTTPS
  • all pure Go!
37

Controlling flying drones

www.godrone.io

38

Emulators

Go: The Language For Emulators

39

Disassembler, linker, compiler.

  • llgo compiler (Go -> LLVM IR -> ...)
  • Go 1.3's disassembler
  • Go 1.4's linker
  • Go 1.4 or Go 1.5's compiler
40

Mobile

  • Go runs on ARM
  • minux's iOS port of Go
  • Camlistore child process, goandroid
41

Audio synthesis

  • https://github.com/nf/sigourney
42

Cloud infrastructure

Go: the emerging language of cloud infrastructure

43

Load balancers & servers

  • YouTube's vitess: MySQL
  • net/http/httputil.ReverseProxy
  • dl.google.com
  • etc
44

Raspberry Pi GPIO

github.com/davecheney/gpio

github.com/stianeikeland/go-rpio

github.com/luismesas/goPi

45

What other language is used for all these?

  • web apps
  • scripts
  • system administration
  • image processing
  • load balancers, servers
  • crypto
  • hardware

Go!

46

And even better...

47

Go built-in tools

  • testing
  • benchmarking
  • profiling (CPU, memory, blocking)
  • huge standard library
  • "go get"
  • godoc
  • gofmt, goimports
  • race detector
  • static binaries
48

So why isn't Go perfect?

49

Generics

  • No generics
  • It sucks sometimes, but rarely
  • Use maps, slices, interfaces
  • Occasionally a gross interface{}
  • No algorithms in stdlib
  • No great proposal yet
50

No generics (Simon Peyton Jones: "Haskell is useless")

51

Data races can happen

  • shared, mutable state is possible
  • use channels
  • be careful
  • conventions and documentation help
  • runtime race detector helps too$ go test -race
52

Code generation

  • The default Go compiler often generates pretty dumb code
  • Especially on ARM
  • But getting better
  • In Go 1.4, 1.5+: Go compiler in Go, refactor, add SSA, more optimizations
53

gccgo

  • gccgo generates very good code
  • ... but lacks escape analysis: kills performance with many small allocs + garbage
  • ... GC isn't precise. Bad for 32-bit.
54

Compiling to JavaScript isn't yet great

  • Would be nice to write frontend & backend in the same language, share code
  • go/types + go/ssa enables many things:

TARDIS Go Compiler

55

Limited Mobile Support

  • No built-in Android support (but goandroid, child processes...)
  • No built-in iOS support (but unmerged port...)
56

Embedding Go in C/C++/Java/etc

  • Can't embed Go in other languages, environments.
  • e.g. Android, iOS, C/C++ programs
  • problems with memory setup, GC, signals, ...
  • need embedding API design
  • In Go 1.4?
57

Shared libraries

  • no shared libraries
  • can't load Go code at runtime
58

Garbage collector

  • Pauses: faster each release, good people working on it.
  • Precision: heap in 1.2, most stacks in 1.3, more in 1.4.
  • 4 versions of memcached: Perl, C, C++, Go
59

Hot stack splits

  • Almost entirely fixed in Go 1.3's contiguous stacks
  • Rest should be in Go 1.4.
60

But...

  • Problems are fixable.
  • It all keeps getting better with each release.
61

Go for everything

  • Since mid-2010, I prefer 90%-perfect Go for all my hacking:
  • Go is flexible, fun, readable
  • Go is fast (for computers & humans)
  • Go is good for working with others
  • Go for everything!
62

Thank you

Brad Fitzpatrick

@bradfitz

bradfitz@golang.org

http://bradfitz.com

http://camlistore.org

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)

[8]ページ先頭

©2009-2025 Movatter.jp