CS 563 Concurrent Programming
Lecture 7: Go
Design began in 2007 at Google by
Copyright By PowCoder代写 加微信 powcoder
, , Became open source in 2009
Statically typed, very fast compilation
C-like syntax
Built-in concurrency
Garbage collection
No classes or type inheritance
Motivation
Go is a programming language designed to help solve Google’s problems What problems?
C++ for servers, plus lots of Java and Python Thousands of engineers
Gazillions of lines of code
Distributed build system
Lots of machines
Go was designed by and for people who write, read, debug and maintain large software systems
Who Uses Go
Influences
Primitive types
bool, string, int, unit, float, etc. Containers
arrays, slices, maps Structs
Concurrency in Go
A Go function or a method executing concurrently in the same address space as other goroutines
A running program consists of one or more goroutines
Starting a goroutine
Invoke a function or method: say go
Some simple facts
Goroutines are cheap
Goroutines can run concurrently on different processors, sharing memory
Goroutines can communicate with each other
Channels in Go
Go has a type called a channel that provides communication and synchronization capabilities
It also has special control structures that build on channels to make concurrent programming easy
The channel type
In its simplest form the type looks like:
chan elementType
With a value of this type, you can send and receive items of elementType
Channels are a reference type, which means if you assign one chan variable to another, both variables access the same channel
Use make to allocate a channel:
make(chan int)
The communication operator: <- The arrow points in the direction of data flow As a binary operator, <- sends the value on the right to the channel on the left As a prefix unary operator, <- receives from a channel: By default, communication is synchronous A send operation on a channel blocks until a receiver is available for the same channel A receive operation for a channel blocks until a sender is available for the same channel Communication is therefore a form of synchronization Two goroutines exchanging data through a channel synchronize at the moment of communication Synchronous/Asynchronous channel Synchronous channels are unbuffered. Sends do not complete until a receiver has accepted the value c := make(chan int) Asynchronous channels are buffered, asynchronous channel that can be created by telling make the number of elements in the buffer c := make(chan int, 50) Select is a control structure in Go, similar to the guarded communication in CSP Select executes one runnable case at random. If no case is runnable, it blocks until one is. A default clause is always runnable Select semantics Each case must be a communication All channel expressions are evaluated All expressions to be sent are evaluated If any communication can proceed, it does; others are ignored If multiple cases are ready, one is selected at random; others do not execute Otherwise: If there is a default clause, that runs If there is no default clause, select statement blocks until one communication can proceed; there is no re-evaluation of channels or values Testing for communicability Can a communication proceed without blocking? Can a communication succeed in a given time interval? Concurrency issues Go tries to take care of lots of concurrency issues: Channel send and receive are atomic Select statement is very carefully defined and implemented Channels give you both synchronization and communication The rule of thumb: Do not communicate by sharing memory, instead, share memory by communicating Example: Google Search What does Google search do? Given a query, return a page of search results How do we get the search results? Send the query to web search, image search, YouTube, maps, news, etc., then mix the results How to implement this in a scalable, robust way? Golang: https://golang.org A Tour of Go: https://tour.golang.org/list Effective Go: https://golang.org/doc/effective_go.html Go by example: https://gobyexample.com 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com