SlideShare a Scribd company logo
Concurrent Programming in Go
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 1/32
The Need for Concurrency
Due to physical limits, CPU clock speed has basically been frozen since the early
2000's.
Moore's law is still operational and has resulted in on-chip hardware expanding
dramatically. In particular, CPU chips contain multiple cores which allow multiple
parallel threads of execution.
To obtain better performance, programs need to exploit the multiple cores using
techniques for concurrent execution.
Concurrent: multiple threads of control can be active at the same time.
Parallel: multiple threads of control execute simultaneously. Requires multiple
cores.
Performance is not the only motivation for concurrency; sometimes a solution is
most conveniently expressed as concurrent computations.
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 2/32
Concurrency Models
[Taken from Kevlin Henney, Thinking Outside the Synchronization Quadrant, NDC
2017.]
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 3/32
Concurrency Models Continued
A major problem in concurrent programs is mutable shared state. Multiple models for
concurrency include:
Shared Mutable State
Leads to lock-based programming with synchronization primitives like mutexes
and semaphores used to protect shared data. Synchronization is a global property
of the program. Very low-level and error-prone but very popular.
Shared Immutable State
Leads to functional programming exemplified by Haskell or Clojure. Persistent
data (old versions of data always available as long as needed).
Mutable State, No Sharing
Actors model. No shared data. All communication is via passing of immutable
messages. More object-oriented than so-called object-oriented programming.
Golang.
Immutable State, No Sharing
All communication via passing messages between threads which never modify
existing data. Exemplified by Erlang, Elixir.
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 4/32
Go Features
Imperative language.
Only single keyword for all iteration: for.
Types in declarations can be inferred from initializers.
Short declarations.
No need for semicolons except within for statements.
Similar to C in that there is no character data type, rune alias for int32 for
Unicode code points, but string and character literals.
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 5/32
Go Features Continued
Garbage collection.
Error values.
Arrays with constant sizes.
Slices.
Strings are constant byte slices.
No classes, but structurally typed interfaces without needing any implements
relationship.
Concurrency without shared state with channels used for passing data.
More conventional concurrency with shared state.
Batteries included.
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 6/32
Go Hello World
In hello1.go:
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
Log:
$ ./hello1
hello world
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 7/32
Yet Another Hello
In hello2.go:
package main
import (
"fmt"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("usage: %s GREETEE...n",
os.Args[0])
os.Exit(1)
}
for _, greetee := range os.Args[1:] {
fmt.Printf("hello %sn", greetee)
}
}
Log:
$ ./hello2 tom sue
hello tom
hello sue
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 8/32
Lissajous GIF in Browser
Use net/http and image library functions.
In main.go and lissajous.go.
Start HTTP server on local machine on port 8000:
$ ./lissajous 8000
Then visit http://localhost:8000/?f=1&p=0.1 where f is the relative frequency of wrt
and p is the phase inc for each iteration.
y x
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 9/32
Concurrent URL Fetch using Go Routines
In fetchall.go
Log:
$ ./fetchall https://golang.org 
https://www.binghamton.edu 
https://gopl.io 
https://godoc.org
0.47s 23662 https://www.binghamton.edu
0.73s 62384 https://golang.org
0.77s 32284 https://godoc.org
0.85s 4154 https://gopl.io
0.85s elapsed
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 10/32
Go Primitive Data
Boolean type bool with literals true and false.
Declarations:
var cond bool; //initialized to false
var toBe = false; //type inferred from initializer
isDone := false; //short declaration
Integer types int, int8, int16 int32, int64. Also corresponding unsigned types
uint, uint8, ; literals in decimal, hexadecimal with 0[xX] prefix, octal with 0
or 0[oO] prefix, binary with 0[bB] prefix. Internal underscores for readability. The
following are all equivalent: 12_34, 0x4d2, 0o2322 or 02322,
0b0100_1101_0010.
A byte is an alias for a uint8 and rune an alias for int32 (representing a unicode
code point).
Declarations:
var i int //initialized to 0
var MaxInt64 uint64 = 1<<64 - 1
age := 42 //short declaration
IEEE 754 floating point types float32 and float64. Literals in decimal or
hexadecimal. Internal underscores for readability. For hexadecimal literals, the
exponent part after p or P scales the mantissa by a power-of-2. Examples: 0., .1e5,
0x2.0p5 (== 64), 0x1.Fp+0 (== 1.9375).
Complex types complex64, complex128.
…
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 11/32
If Statements
Traditional if-then and if-then-else statements.
No parentheses around condition; braces required around statements.
Can precede condition by a declaration.
if x := f(); x % 2 == 0 {
fmt.Print(x*3)
}
else if y := g(x); y < 0 {
fmt.Print(x - y)
}
else {
fmt.Println(x + y)
}
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 12/32
Switch Statements
Basically a multi-branch if. No default fall-through behavior.
Basic switch:
switch coinflip() {
case "heads":
heads++
case "tails":
tails++
default:
fmt.Print("coin landed on edge!")
}
A tagless switch does not have an operand, simply test cases:
func Signum(x int) int {
switch {
case x > 0:
return +1
case x < 0:
return -1
default:
return 0
}
}
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 13/32
Type Switch
Can switch on the type of an expression:
var z int
switch t := x.(type) {
case int:
z = t*2
case string:
z = len(t)*2
default:
z = 1
}
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 14/32
Loops
Uses single keyword for:
C style for-loop: for inits; cond; updates { ... }
No parentheses; must have braces around body.
Omit inits and updates for a while-loop.
Omit inits, cond and updates for a forever loop.
Use for-range to iterate over a range of integers or key-value pairs of a collection.
Allows break and continue statements.
Example loops.go
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 15/32
Arrays
Size of array must be known at compile time (no equivalent of C's variable-length
arrays VLAs).
Two arrays of different sizes are of different types.
Unlike C, always passed by value unless caller explicitly passes a pointer.
const n = 10
var arr [n]string //array of 10 "" strings
//[...] means # of elements fixed by initializer
days [...]string{
"mo", "tu", "we", "th", "fr", "sa", "su",
}
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 16/32
Slices
Allows using arrays with dynamic sizing.
A slice for base type T looks like:
struct {
T *arr //backing array
int len, capacity
}
Declaration looks like an array declaration, but no size specification. For example,
var bytes []byte will declare bytes as a nil byte-slice.
words := []string{"hello", "world"} will declare words as a slice into a
string array, wih len(word) == cap(words) == 2.
ints := make([]int, 3, 5) will declare ints as a slice into an int array
with len(ints) == 3 and cap(ints) == 5. The first len elements of the slice
will be initialized to 0.
Very efficient subslicing: given slice a, a[lo:hi], a[lo:], a[:hi] returns sub-
slices with inclusive lo bound and exclusive hi bound.
Slices share the underlying array entries. Example days.go:
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 17/32
Appending to a Slice
If sliceT []T and v1 T, , vn T, then append(sliceT, v1, ..., vn)
returns sliceT with v1, , vn appended onto its end.
If an append() requires exceeding the capacity of the original backing array, then
the backing array is reallocated.
append.go shows that appending reallocates underlying array so that backing array
changes.
copy(destSlice, srcSlice) copies srcSlice to destSlice.
…
…
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 18/32
Strings
A string is an immutable slice of bytes. Since byte slices are mutable, use []byte
slices or bytes.Buffer() for efficient string mutation.
May contain a '0' bytes.
Often interpreted as UTF-8 encoded sequence of Unicode code points. Go's range
loop over a string will process a character at a time (rather than a byte at a time).
More flexibility from unicode/utf8 package. Unicode replacement character
ufffd � produced for invalid utf8 encoding.
len(s) returns # of bytes (not runes) in string s.
s[i] returns i'th byte with a panic if 0 < i || i >= len(s). Cast string to a
rune array to get i'th character: []rune(s)[i].
String concatentation using + and += produce new strings.
Lexicographical string comparison using usual relational operators.
Since strings are immutable, slice operations s[i:j] (substrings) are efficient since
they share the same byte sequence.
String literals within " can contain conventional C-style escape sequences as well as
uhhhh or Uhhhhhhhh for 16-bit or 32-bit Unicode code points.
Raw string literals enclosed within ` (back-quotes) can contain any characters other
than back-quote. Carriage returns r are deleted. Useful for regexs, HTML
templates and JSON literals, etc.
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 19/32
Characters versus Bytes
From strbytes.go
func main() {
hellos := map[string]string{
"en": "Hello World!", //english
"es": "¡Hola Mundo!", //spanish
"hi": "नमस्ते दुनिया!", //hindi
"ja": "こんにちは、 ", //japanese
"pt": "Olá Mundo!", //portuguese
"zh": " !", //chinese
}
for code,greet := range hellos {
fmt.Printf("%s: bytes[%d] = % x; " +
"chars[%d] = %qn",
code, len(greet), greet,
utf8.RuneCountInString(greet),
[]rune(greet))
}
}
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 20/32
Characters versus Bytes: Log
Edited Log:
$ ./strbytes
zh: bytes[13] = e4 b8 96 e7 95 8c e6 82 a8 e5 a5 bd 21; 
chars[5] = [' ' ' ' ' ' ' ' '!']
en: bytes[12] = 48 65 6c 6c 6f 20 57 6f 72 6c 64 21; 
chars[12] = ['H' 'e' 'l' 'l' 'o' ' ' 
'W' 'o' 'r' 'l' 'd' '!']
es: bytes[13] = c2 a1 48 6f 6c 61 20 4d 75 6e 64 6f 21;
chars[12] = ['¡' 'H' 'o' 'l' 'a' ' ' 
'M' 'u' 'n' 'd' 'o' '!']
hi: bytes[38] = e0 a4 a8 e0 a4 ae e0 a4 b8 e0 a5 8d e0 
a4 a4 e0 a5 87 20 e0 a4 a6 e0 a5 81 e0 a4 
a8 e0 a4 bf e0 a4 af e0 a4 be 21; 
chars[14] = ['न' 'म' 'स' '् ' 'त' 'े ' ' ' 
'द' 'ु ' 'न' 'ि' 'य' 'ा' '!']
ja: bytes[25] = e3 81 93 e3 82 93 e3 81 ab e3 81 a1 e3 81 
af e3 80 81 20 e4 b8 96 e7 95 8c; 
chars[9] = ['こ' 'ん' 'に' 'ち' 'は' '、' ' ' ' ' ' ']
pt: bytes[11] = 4f 6c c3 a1 20 4d 75 6e 64 6f 21; 
chars[10] = ['O' 'l' 'á' ' ' 'M' 'u' 'n' 'd' 'o' '!']
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 21/32
Maps
Go supports maps from any type which implements equality to an arbitrary type.
Syntax for type is map[K]V where K is the type of the key and V is the type of
value. Examples:
var counts map[string]int
colorSpecs := make(map[string]string)
colorIndexes := map[string]int{"black": 0, "red": 1}
for-range loops over key-value pairs. Given the above colorIndexes:
for c, i := range colorIndexes {
fmt.Printf("%q: %dn", c, i)
}
// "black": 0
// "red": 1
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 22/32
Pointers
Variables which hold addresses.
Cannot take address of a slice element. Why?
Introduces aliasing.
No pointer arithmetic.
i, j := 33, 42
p := &i
j = *p; //j == 33
*p = 99 //i == 99
fmt.Println(i, j) //99, 33
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 23/32
Structures
Collection of heterogenous fields.
type Circle struct {
Label string
X, Y int
Radius int
}
Note uppercase on field names to allow access outside package.
Can refer to fields using . notation for both struct values and struct pointers.
c1 := Circle{Label: "c1", X: 1, Y: 1}
c1P := &c1
fmt.Printf("struct circle %s at (%g, %g)n",
c1.label, c1.X, c1.Y)
fmt.Printf("pointed to circle %s at (%g, %g)n",
c1P.label, c1P.X, c1P.Y)
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 24/32
Structure Embedding
Can embed structures anonymously:
type Point struct { X, Y int }
type Circle struct {
Point
Radius string
}
type Rect struct {
Point
Width, Height int
}
var c1 Circle //initialized with "zero" values
c1.X = 1 //ok: c1.Point.X = 1
c1.Y = 1 //ok: c1.Point.Y = 1
//cannot initialize through anon struct directly
c1 = { X: 2, Y: 2 } //error, must use intermediate
c2 = { Point{X: 2, Y: 2}, Radius: 2 } //ok
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 25/32
Goroutines
Simply precede function call with go keyword, to run call in a separate goroutine.
Goroutines are ike a thread, but very lightweight. Can easily have tens or hundreds
of thousands.
An example which uses a goroutine to display a spinner while performing a
computation: fib-spinner.go.
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 26/32
Channels
Channels are used for communicating between and synchronizing goroutines.
Like Unix pipes but Unix pipes are untyped byte streams, whereas go channels
transmit typed data.
Declare a channel having element type T using chan T and create using make():
ch := make(chan string) //ch has type chan string
A channel can be both read and written by a goroutime, but typically a goroutine
only reads a channel or only writes a channel. We can declare a readonly int
channel as <-chan int and a writeonly int channel as chan<- int.
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 27/32
Channel Example
First goroutine converts command-line args to int.
Subsequent goroutines compute function.
Main goroutine outputs.
Termination because each goroutine closes output channel.
1 2 3 4 go
os.Args[1:]
go
n + 1
go
2 * n
go
n * n
main
output
16 36 64 100
In fn-pipes.go
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 28/32
Multiplexing using select
We may need to receive from first among several channels which has data without
blocking on the others.
Use the select statement:
select {
case <- ch1:
// ch1 had discarded data
case x <- ch2:
// process x read from ch1
...
case ch3 <- y:
// ch3 was able to receive y
default:
// no channel was ready
}
select {} blocks for ever.
If multiple channels are ready, select will choose one at random, to ensure
fairness.
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 29/32
Countdown Timer
In countdown.go
func main() {
fmt.Println("counting down ...")
ticker := time.NewTicker(1 * time.Second)
//don't care about value
abort := make(chan struct{})
go func() { //reads garbage from stdin
os.Stdin.Read(make([]byte, 1))
abort <- struct{}{}
}() //called IIFE in JS
for count := 5; count > 0; count-- {
select {
case <- ticker.C: //ticker.C is chan
fmt.Printf("%d... ", count)
case <- abort:
fmt.Println("ABORTED!")
return
}
}
fmt.Println("LAUNCH!")
}
Log:
$ ./countdown
counting down ...
5... 4... 3... 2... 1... LAUNCH!
$ ./countdown
counting down ...
5... 4... # typed input to console
ABORTED!
$
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 30/32
Not Covered
Interfaces.
Buffered channels.
Goroutine cancellation.
Concurrency with shared data: mutexes.
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 31/32
References
Alan A. A. Donovan, Brian W. Kernighan, The Go Programming Language, 1st Edition,
Addison-Wesley, 2016.
11/27/24, 12:28 PM Concurrent Programming in Go
https://zdu.binghamton.edu/cs571/slides/golang/golang.html 32/32
Ad

Recommended

Go Lang Tutorial
Go Lang Tutorial
Wei-Ning Huang
 
Golang
Golang
Fatih Şimşek
 
Golang
Golang
Software Infrastructure
 
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / Overview
Markus Schneider
 
Go Programming language, golang
Go Programming language, golang
Basil N G
 
Go Programming Language (Golang)
Go Programming Language (Golang)
Ishin Vin
 
Something about Golang
Something about Golang
Anton Arhipov
 
Introduction to Programming in Go
Introduction to Programming in Go
Amr Hassan
 
Golang 101 (Concurrency vs Parallelism)
Golang 101 (Concurrency vs Parallelism)
Pramesti Hatta K.
 
Coding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBM
Raveen Perera
 
The GO programming language
The GO programming language
Marco Sabatini
 
Beauty and Power of Go
Beauty and Power of Go
Frank Müller
 
Go ahead, make my day
Go ahead, make my day
Tor Ivry
 
The GO Language : From Beginners to Gophers
The GO Language : From Beginners to Gophers
I.I.S. G. Vallauri - Fossano
 
Golang
Golang
Felipe Mamud
 
Introduction to Go ProgrammingLanguage.ppt
Introduction to Go ProgrammingLanguage.ppt
PedroAlexandre215482
 
Inroduction to golang
Inroduction to golang
Yoni Davidson
 
Should i Go there
Should i Go there
Shimi Bandiel
 
2011 july-nyc-gtug-go
2011 july-nyc-gtug-go
ikailan
 
Ready to go
Ready to go
Atin Mukherjee
 
Introduction to go language programming
Introduction to go language programming
Mahmoud Masih Tehrani
 
ProgrammingwithGOLang
ProgrammingwithGOLang
Shishir Dwivedi
 
Lecture 1 - Overview of Go Language 1.pdf
Lecture 1 - Overview of Go Language 1.pdf
daomaithuhuyen1273
 
LCA2014 - Introduction to Go
LCA2014 - Introduction to Go
dreamwidth
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) Language
Aniruddha Chakrabarti
 
Google Go Overview
Google Go Overview
Moritz Haarmann
 
Go introduction
Go introduction
Anna Goławska
 
Fundamental concurrent programming
Fundamental concurrent programming
Dimas Prawira
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
gowthamvicky1
 

More Related Content

Similar to Concurrent Programming in Go basics and programming (20)

Golang 101 (Concurrency vs Parallelism)
Golang 101 (Concurrency vs Parallelism)
Pramesti Hatta K.
 
Coding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBM
Raveen Perera
 
The GO programming language
The GO programming language
Marco Sabatini
 
Beauty and Power of Go
Beauty and Power of Go
Frank Müller
 
Go ahead, make my day
Go ahead, make my day
Tor Ivry
 
The GO Language : From Beginners to Gophers
The GO Language : From Beginners to Gophers
I.I.S. G. Vallauri - Fossano
 
Golang
Golang
Felipe Mamud
 
Introduction to Go ProgrammingLanguage.ppt
Introduction to Go ProgrammingLanguage.ppt
PedroAlexandre215482
 
Inroduction to golang
Inroduction to golang
Yoni Davidson
 
Should i Go there
Should i Go there
Shimi Bandiel
 
2011 july-nyc-gtug-go
2011 july-nyc-gtug-go
ikailan
 
Ready to go
Ready to go
Atin Mukherjee
 
Introduction to go language programming
Introduction to go language programming
Mahmoud Masih Tehrani
 
ProgrammingwithGOLang
ProgrammingwithGOLang
Shishir Dwivedi
 
Lecture 1 - Overview of Go Language 1.pdf
Lecture 1 - Overview of Go Language 1.pdf
daomaithuhuyen1273
 
LCA2014 - Introduction to Go
LCA2014 - Introduction to Go
dreamwidth
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) Language
Aniruddha Chakrabarti
 
Google Go Overview
Google Go Overview
Moritz Haarmann
 
Go introduction
Go introduction
Anna Goławska
 
Fundamental concurrent programming
Fundamental concurrent programming
Dimas Prawira
 
Golang 101 (Concurrency vs Parallelism)
Golang 101 (Concurrency vs Parallelism)
Pramesti Hatta K.
 
Coding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBM
Raveen Perera
 
The GO programming language
The GO programming language
Marco Sabatini
 
Beauty and Power of Go
Beauty and Power of Go
Frank Müller
 
Go ahead, make my day
Go ahead, make my day
Tor Ivry
 
Introduction to Go ProgrammingLanguage.ppt
Introduction to Go ProgrammingLanguage.ppt
PedroAlexandre215482
 
Inroduction to golang
Inroduction to golang
Yoni Davidson
 
2011 july-nyc-gtug-go
2011 july-nyc-gtug-go
ikailan
 
Introduction to go language programming
Introduction to go language programming
Mahmoud Masih Tehrani
 
Lecture 1 - Overview of Go Language 1.pdf
Lecture 1 - Overview of Go Language 1.pdf
daomaithuhuyen1273
 
LCA2014 - Introduction to Go
LCA2014 - Introduction to Go
dreamwidth
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) Language
Aniruddha Chakrabarti
 
Fundamental concurrent programming
Fundamental concurrent programming
Dimas Prawira
 

Recently uploaded (20)

Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
gowthamvicky1
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Pavement and its types, Application of rigid and Flexible Pavements
Pavement and its types, Application of rigid and Flexible Pavements
Sakthivel M
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
20CE601- DESIGN OF STEEL STRUCTURES ,INTRODUCTION AND ALLOWABLE STRESS DESIGN
gowthamvicky1
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Pavement and its types, Application of rigid and Flexible Pavements
Pavement and its types, Application of rigid and Flexible Pavements
Sakthivel M
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
Ad

Concurrent Programming in Go basics and programming

  • 1. Concurrent Programming in Go 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 1/32 The Need for Concurrency Due to physical limits, CPU clock speed has basically been frozen since the early 2000's. Moore's law is still operational and has resulted in on-chip hardware expanding dramatically. In particular, CPU chips contain multiple cores which allow multiple parallel threads of execution. To obtain better performance, programs need to exploit the multiple cores using techniques for concurrent execution. Concurrent: multiple threads of control can be active at the same time. Parallel: multiple threads of control execute simultaneously. Requires multiple cores. Performance is not the only motivation for concurrency; sometimes a solution is most conveniently expressed as concurrent computations. 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 2/32
  • 2. Concurrency Models [Taken from Kevlin Henney, Thinking Outside the Synchronization Quadrant, NDC 2017.] 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 3/32 Concurrency Models Continued A major problem in concurrent programs is mutable shared state. Multiple models for concurrency include: Shared Mutable State Leads to lock-based programming with synchronization primitives like mutexes and semaphores used to protect shared data. Synchronization is a global property of the program. Very low-level and error-prone but very popular. Shared Immutable State Leads to functional programming exemplified by Haskell or Clojure. Persistent data (old versions of data always available as long as needed). Mutable State, No Sharing Actors model. No shared data. All communication is via passing of immutable messages. More object-oriented than so-called object-oriented programming. Golang. Immutable State, No Sharing All communication via passing messages between threads which never modify existing data. Exemplified by Erlang, Elixir. 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 4/32
  • 3. Go Features Imperative language. Only single keyword for all iteration: for. Types in declarations can be inferred from initializers. Short declarations. No need for semicolons except within for statements. Similar to C in that there is no character data type, rune alias for int32 for Unicode code points, but string and character literals. 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 5/32 Go Features Continued Garbage collection. Error values. Arrays with constant sizes. Slices. Strings are constant byte slices. No classes, but structurally typed interfaces without needing any implements relationship. Concurrency without shared state with channels used for passing data. More conventional concurrency with shared state. Batteries included. 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 6/32
  • 4. Go Hello World In hello1.go: package main import "fmt" func main() { fmt.Println("hello world") } Log: $ ./hello1 hello world 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 7/32 Yet Another Hello In hello2.go: package main import ( "fmt" "os" ) func main() { if len(os.Args) < 2 { fmt.Printf("usage: %s GREETEE...n", os.Args[0]) os.Exit(1) } for _, greetee := range os.Args[1:] { fmt.Printf("hello %sn", greetee) } } Log: $ ./hello2 tom sue hello tom hello sue 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 8/32
  • 5. Lissajous GIF in Browser Use net/http and image library functions. In main.go and lissajous.go. Start HTTP server on local machine on port 8000: $ ./lissajous 8000 Then visit http://localhost:8000/?f=1&p=0.1 where f is the relative frequency of wrt and p is the phase inc for each iteration. y x 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 9/32 Concurrent URL Fetch using Go Routines In fetchall.go Log: $ ./fetchall https://golang.org https://www.binghamton.edu https://gopl.io https://godoc.org 0.47s 23662 https://www.binghamton.edu 0.73s 62384 https://golang.org 0.77s 32284 https://godoc.org 0.85s 4154 https://gopl.io 0.85s elapsed 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 10/32
  • 6. Go Primitive Data Boolean type bool with literals true and false. Declarations: var cond bool; //initialized to false var toBe = false; //type inferred from initializer isDone := false; //short declaration Integer types int, int8, int16 int32, int64. Also corresponding unsigned types uint, uint8, ; literals in decimal, hexadecimal with 0[xX] prefix, octal with 0 or 0[oO] prefix, binary with 0[bB] prefix. Internal underscores for readability. The following are all equivalent: 12_34, 0x4d2, 0o2322 or 02322, 0b0100_1101_0010. A byte is an alias for a uint8 and rune an alias for int32 (representing a unicode code point). Declarations: var i int //initialized to 0 var MaxInt64 uint64 = 1<<64 - 1 age := 42 //short declaration IEEE 754 floating point types float32 and float64. Literals in decimal or hexadecimal. Internal underscores for readability. For hexadecimal literals, the exponent part after p or P scales the mantissa by a power-of-2. Examples: 0., .1e5, 0x2.0p5 (== 64), 0x1.Fp+0 (== 1.9375). Complex types complex64, complex128. … 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 11/32 If Statements Traditional if-then and if-then-else statements. No parentheses around condition; braces required around statements. Can precede condition by a declaration. if x := f(); x % 2 == 0 { fmt.Print(x*3) } else if y := g(x); y < 0 { fmt.Print(x - y) } else { fmt.Println(x + y) } 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 12/32
  • 7. Switch Statements Basically a multi-branch if. No default fall-through behavior. Basic switch: switch coinflip() { case "heads": heads++ case "tails": tails++ default: fmt.Print("coin landed on edge!") } A tagless switch does not have an operand, simply test cases: func Signum(x int) int { switch { case x > 0: return +1 case x < 0: return -1 default: return 0 } } 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 13/32 Type Switch Can switch on the type of an expression: var z int switch t := x.(type) { case int: z = t*2 case string: z = len(t)*2 default: z = 1 } 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 14/32
  • 8. Loops Uses single keyword for: C style for-loop: for inits; cond; updates { ... } No parentheses; must have braces around body. Omit inits and updates for a while-loop. Omit inits, cond and updates for a forever loop. Use for-range to iterate over a range of integers or key-value pairs of a collection. Allows break and continue statements. Example loops.go 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 15/32 Arrays Size of array must be known at compile time (no equivalent of C's variable-length arrays VLAs). Two arrays of different sizes are of different types. Unlike C, always passed by value unless caller explicitly passes a pointer. const n = 10 var arr [n]string //array of 10 "" strings //[...] means # of elements fixed by initializer days [...]string{ "mo", "tu", "we", "th", "fr", "sa", "su", } 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 16/32
  • 9. Slices Allows using arrays with dynamic sizing. A slice for base type T looks like: struct { T *arr //backing array int len, capacity } Declaration looks like an array declaration, but no size specification. For example, var bytes []byte will declare bytes as a nil byte-slice. words := []string{"hello", "world"} will declare words as a slice into a string array, wih len(word) == cap(words) == 2. ints := make([]int, 3, 5) will declare ints as a slice into an int array with len(ints) == 3 and cap(ints) == 5. The first len elements of the slice will be initialized to 0. Very efficient subslicing: given slice a, a[lo:hi], a[lo:], a[:hi] returns sub- slices with inclusive lo bound and exclusive hi bound. Slices share the underlying array entries. Example days.go: 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 17/32 Appending to a Slice If sliceT []T and v1 T, , vn T, then append(sliceT, v1, ..., vn) returns sliceT with v1, , vn appended onto its end. If an append() requires exceeding the capacity of the original backing array, then the backing array is reallocated. append.go shows that appending reallocates underlying array so that backing array changes. copy(destSlice, srcSlice) copies srcSlice to destSlice. … … 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 18/32
  • 10. Strings A string is an immutable slice of bytes. Since byte slices are mutable, use []byte slices or bytes.Buffer() for efficient string mutation. May contain a '0' bytes. Often interpreted as UTF-8 encoded sequence of Unicode code points. Go's range loop over a string will process a character at a time (rather than a byte at a time). More flexibility from unicode/utf8 package. Unicode replacement character ufffd � produced for invalid utf8 encoding. len(s) returns # of bytes (not runes) in string s. s[i] returns i'th byte with a panic if 0 < i || i >= len(s). Cast string to a rune array to get i'th character: []rune(s)[i]. String concatentation using + and += produce new strings. Lexicographical string comparison using usual relational operators. Since strings are immutable, slice operations s[i:j] (substrings) are efficient since they share the same byte sequence. String literals within " can contain conventional C-style escape sequences as well as uhhhh or Uhhhhhhhh for 16-bit or 32-bit Unicode code points. Raw string literals enclosed within ` (back-quotes) can contain any characters other than back-quote. Carriage returns r are deleted. Useful for regexs, HTML templates and JSON literals, etc. 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 19/32 Characters versus Bytes From strbytes.go func main() { hellos := map[string]string{ "en": "Hello World!", //english "es": "¡Hola Mundo!", //spanish "hi": "नमस्ते दुनिया!", //hindi "ja": "こんにちは、 ", //japanese "pt": "Olá Mundo!", //portuguese "zh": " !", //chinese } for code,greet := range hellos { fmt.Printf("%s: bytes[%d] = % x; " + "chars[%d] = %qn", code, len(greet), greet, utf8.RuneCountInString(greet), []rune(greet)) } } 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 20/32
  • 11. Characters versus Bytes: Log Edited Log: $ ./strbytes zh: bytes[13] = e4 b8 96 e7 95 8c e6 82 a8 e5 a5 bd 21; chars[5] = [' ' ' ' ' ' ' ' '!'] en: bytes[12] = 48 65 6c 6c 6f 20 57 6f 72 6c 64 21; chars[12] = ['H' 'e' 'l' 'l' 'o' ' ' 'W' 'o' 'r' 'l' 'd' '!'] es: bytes[13] = c2 a1 48 6f 6c 61 20 4d 75 6e 64 6f 21; chars[12] = ['¡' 'H' 'o' 'l' 'a' ' ' 'M' 'u' 'n' 'd' 'o' '!'] hi: bytes[38] = e0 a4 a8 e0 a4 ae e0 a4 b8 e0 a5 8d e0 a4 a4 e0 a5 87 20 e0 a4 a6 e0 a5 81 e0 a4 a8 e0 a4 bf e0 a4 af e0 a4 be 21; chars[14] = ['न' 'म' 'स' '् ' 'त' 'े ' ' ' 'द' 'ु ' 'न' 'ि' 'य' 'ा' '!'] ja: bytes[25] = e3 81 93 e3 82 93 e3 81 ab e3 81 a1 e3 81 af e3 80 81 20 e4 b8 96 e7 95 8c; chars[9] = ['こ' 'ん' 'に' 'ち' 'は' '、' ' ' ' ' ' '] pt: bytes[11] = 4f 6c c3 a1 20 4d 75 6e 64 6f 21; chars[10] = ['O' 'l' 'á' ' ' 'M' 'u' 'n' 'd' 'o' '!'] 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 21/32 Maps Go supports maps from any type which implements equality to an arbitrary type. Syntax for type is map[K]V where K is the type of the key and V is the type of value. Examples: var counts map[string]int colorSpecs := make(map[string]string) colorIndexes := map[string]int{"black": 0, "red": 1} for-range loops over key-value pairs. Given the above colorIndexes: for c, i := range colorIndexes { fmt.Printf("%q: %dn", c, i) } // "black": 0 // "red": 1 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 22/32
  • 12. Pointers Variables which hold addresses. Cannot take address of a slice element. Why? Introduces aliasing. No pointer arithmetic. i, j := 33, 42 p := &i j = *p; //j == 33 *p = 99 //i == 99 fmt.Println(i, j) //99, 33 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 23/32 Structures Collection of heterogenous fields. type Circle struct { Label string X, Y int Radius int } Note uppercase on field names to allow access outside package. Can refer to fields using . notation for both struct values and struct pointers. c1 := Circle{Label: "c1", X: 1, Y: 1} c1P := &c1 fmt.Printf("struct circle %s at (%g, %g)n", c1.label, c1.X, c1.Y) fmt.Printf("pointed to circle %s at (%g, %g)n", c1P.label, c1P.X, c1P.Y) 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 24/32
  • 13. Structure Embedding Can embed structures anonymously: type Point struct { X, Y int } type Circle struct { Point Radius string } type Rect struct { Point Width, Height int } var c1 Circle //initialized with "zero" values c1.X = 1 //ok: c1.Point.X = 1 c1.Y = 1 //ok: c1.Point.Y = 1 //cannot initialize through anon struct directly c1 = { X: 2, Y: 2 } //error, must use intermediate c2 = { Point{X: 2, Y: 2}, Radius: 2 } //ok 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 25/32 Goroutines Simply precede function call with go keyword, to run call in a separate goroutine. Goroutines are ike a thread, but very lightweight. Can easily have tens or hundreds of thousands. An example which uses a goroutine to display a spinner while performing a computation: fib-spinner.go. 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 26/32
  • 14. Channels Channels are used for communicating between and synchronizing goroutines. Like Unix pipes but Unix pipes are untyped byte streams, whereas go channels transmit typed data. Declare a channel having element type T using chan T and create using make(): ch := make(chan string) //ch has type chan string A channel can be both read and written by a goroutime, but typically a goroutine only reads a channel or only writes a channel. We can declare a readonly int channel as <-chan int and a writeonly int channel as chan<- int. 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 27/32 Channel Example First goroutine converts command-line args to int. Subsequent goroutines compute function. Main goroutine outputs. Termination because each goroutine closes output channel. 1 2 3 4 go os.Args[1:] go n + 1 go 2 * n go n * n main output 16 36 64 100 In fn-pipes.go 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 28/32
  • 15. Multiplexing using select We may need to receive from first among several channels which has data without blocking on the others. Use the select statement: select { case <- ch1: // ch1 had discarded data case x <- ch2: // process x read from ch1 ... case ch3 <- y: // ch3 was able to receive y default: // no channel was ready } select {} blocks for ever. If multiple channels are ready, select will choose one at random, to ensure fairness. 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 29/32 Countdown Timer In countdown.go func main() { fmt.Println("counting down ...") ticker := time.NewTicker(1 * time.Second) //don't care about value abort := make(chan struct{}) go func() { //reads garbage from stdin os.Stdin.Read(make([]byte, 1)) abort <- struct{}{} }() //called IIFE in JS for count := 5; count > 0; count-- { select { case <- ticker.C: //ticker.C is chan fmt.Printf("%d... ", count) case <- abort: fmt.Println("ABORTED!") return } } fmt.Println("LAUNCH!") } Log: $ ./countdown counting down ... 5... 4... 3... 2... 1... LAUNCH! $ ./countdown counting down ... 5... 4... # typed input to console ABORTED! $ 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 30/32
  • 16. Not Covered Interfaces. Buffered channels. Goroutine cancellation. Concurrency with shared data: mutexes. 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 31/32 References Alan A. A. Donovan, Brian W. Kernighan, The Go Programming Language, 1st Edition, Addison-Wesley, 2016. 11/27/24, 12:28 PM Concurrent Programming in Go https://zdu.binghamton.edu/cs571/slides/golang/golang.html 32/32