Wonders of Golang
-
Upload
kartik-sura -
Category
Software
-
view
449 -
download
0
Transcript of Wonders of Golang
![Page 1: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/1.jpg)
Wonders of Golang
![Page 2: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/2.jpg)
Pain points of Systems Software
Change in computing landscape
Scale of development
Developer productivity
Dependency management
![Page 3: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/3.jpg)
![Page 4: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/4.jpg)
![Page 5: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/5.jpg)
Enter Go
A new language, a concurrent, garbage-collected language with fast
compilation.
![Page 6: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/6.jpg)
Syntax and SemanticsSimilar to C• compiled• Statically typed• Procedural with pointers
Small changes to C semantics• No Pointer arithmetic• No implicit numeric conversions• Array bounds are always checked
Big changes• Linguistic support for Concurrency• Garbage collection• Interface, reflection, type switches, etc.
![Page 7: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/7.jpg)
Packages
• Modularity and reusability• Componentize software• Import clause– import “fmt”
• Package name is used to qualify items– Name vs. pkg.Name
• Remote packages– import "github.com/garyburd/redigo"
![Page 8: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/8.jpg)
Defining visibility using Naming
• Name of the identifier itself carries the visibility– Upper case initial letter: Public/ exported/ visible to
other packages– Lower case initial letter: Private to the package
![Page 9: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/9.jpg)
Concurrency
• Do not communicate by sharing memory; instead, share memory by communicating.
![Page 10: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/10.jpg)
Goroutine
• Function executing concurrently with others in same address space
• Lightweight• Multiplexed into multiple OS threads• Go keyword
![Page 11: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/11.jpg)
Channels
• way for two goroutines to communicate with one another and synchronize their execution
• By default, sends and receives block until the other side is ready.
• Combine communication and synchronization• Buffered channels
![Page 12: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/12.jpg)
Channelhttp://www.goinggo.net/2014/02/the-nature-of-channels-in-go.html
![Page 13: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/13.jpg)
Buffered channel
![Page 14: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/14.jpg)
Channel Idioms
• A channel can allow the launching goroutine to wait for the sort to complete.
![Page 15: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/15.jpg)
Server throughput
![Page 16: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/16.jpg)
Request serving architectures
![Page 17: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/17.jpg)
Cost of scheduling OS threads
• POSIX Threads: Signal mask, CPU affinity, cgroups• Store all the CPU registers• Cost of context switch Vs amount of work
![Page 18: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/18.jpg)
Goroutine scheduling
• Switched happen only at predefined times– If channel operations are blocking– Go statement– Blocking syscalls like file and network IO– Garbage collection
• Compiler knows which registers are used
![Page 19: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/19.jpg)
Stack management
POSIX threads• Large amount of
memory pre-reserved• Amount of available
memory reduces with increase in threads
Goroutine• Starts with 2k
• A check before a function call
• Shrinks with GC
![Page 20: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/20.jpg)
OOP
![Page 21: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/21.jpg)
Struct
![Page 22: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/22.jpg)
Embedding
![Page 23: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/23.jpg)
Multiple embedding
![Page 24: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/24.jpg)
A golang-Interface is a class, with NO fields, and ALL VIRTUAL methodsWhen you call a method on the var/parameter, a concrete method is called via method dispatch from a jmp-table.
Interface
![Page 25: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/25.jpg)
Standard library
• Exp package for experimental new packages• Archive and compresion: read .tar and .zip• Bytes and String• Collections: heap, lists• File, OS• Maths• Networking: UNIX domain and network
sockets, TCP/IP, and UDP
![Page 26: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/26.jpg)
![Page 27: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/27.jpg)
Tools
• Gofmt: single style for readability and scalability
• Golint: checks style violations• GoVet: finding common mistakes• Inbuilt performance profiler
![Page 28: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/28.jpg)
![Page 29: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/29.jpg)
Golang @ PubMatic
• Low latency high throughput• Large number of network IO to external
partners• Billions of requests per day• Multiple go services live• Third party components such as redis,
Aerospike, MySQL, GeoIP
![Page 30: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/30.jpg)
We are hiring!http://www.pubmatic.com/careers.php
![Page 31: Wonders of Golang](https://reader036.fdocuments.us/reader036/viewer/2022081422/58749cdb1a28abfc5f8b6609/html5/thumbnails/31.jpg)
GOPHERCON
• The Go Conference in India• 19 - 20 February 2016• Vivanta by Taj, MG Road, Bengaluru • http://www.gophercon.in/• Ticket is Rs 3999 (last 60 tickets remaining)• Discount code D1000 for Rs 1000 off!• https://www.townscript.com/e/gci16