Wonders of Golang

Post on 10-Jan-2017

448 views 0 download

Transcript of Wonders of Golang

Wonders of Golang

Pain points of Systems Software

Change in computing landscape

Scale of development

Developer productivity

Dependency management

Enter Go

A new language, a concurrent, garbage-collected language with fast

compilation.

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.

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"

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

Concurrency

• Do not communicate by sharing memory; instead, share memory by communicating.

Goroutine

• Function executing concurrently with others in same address space

• Lightweight• Multiplexed into multiple OS threads• Go keyword

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

Channelhttp://www.goinggo.net/2014/02/the-nature-of-channels-in-go.html

Buffered channel

Channel Idioms

• A channel can allow the launching goroutine to wait for the sort to complete.

Server throughput

Request serving architectures

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

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

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

OOP

Struct

Embedding

Multiple embedding

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

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

Tools

• Gofmt: single style for readability and scalability

• Golint: checks style violations• GoVet: finding common mistakes• Inbuilt performance profiler

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

We are hiring!http://www.pubmatic.com/careers.php

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