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

More Related Content

DOCX
BEC302_DSDV Lab Manual 3rd sem(1) (1) (2).docx
PPTX
Verilog
PDF
[Apostila] programação arduíno brian w. evans
PPT
PDF
02 c++g3 d
PPT
Os2 2
PDF
24-02-18 Rejender pratap.pdf
PDF
Declare Your Language: What is a Compiler?
BEC302_DSDV Lab Manual 3rd sem(1) (1) (2).docx
Verilog
[Apostila] programação arduíno brian w. evans
02 c++g3 d
Os2 2
24-02-18 Rejender pratap.pdf
Declare Your Language: What is a Compiler?

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

PPTX
Switch case and looping
PDF
L Fu - Dao: a novel programming language for bioinformatics
PPTX
Essentials of Multithreaded System Programming in C++
PDF
OpenCL programming using Python syntax
PDF
Open cl programming using python syntax
PDF
Parallel computation
PDF
Directive-based approach to Heterogeneous Computing
PPTX
My final requirement
PDF
parallel-computation.pdf
PDF
mloc.js 2014 - JavaScript and the browser as a platform for game development
PDF
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScript
PDF
DOUBLE PRECISION FLOATING POINT CORE IN VERILOG
PDF
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
PDF
C++primer
PDF
Skiron - Experiments in CPU Design in D
PPT
basics of optimizations presentation s
PPT
Data structure and algorithm.lect-03.ppt
PPTX
Yeahhhh the final requirement!!!
DOCX
PPT
Os Reindersfinal
Switch case and looping
L Fu - Dao: a novel programming language for bioinformatics
Essentials of Multithreaded System Programming in C++
OpenCL programming using Python syntax
Open cl programming using python syntax
Parallel computation
Directive-based approach to Heterogeneous Computing
My final requirement
parallel-computation.pdf
mloc.js 2014 - JavaScript and the browser as a platform for game development
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScript
DOUBLE PRECISION FLOATING POINT CORE IN VERILOG
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
C++primer
Skiron - Experiments in CPU Design in D
basics of optimizations presentation s
Data structure and algorithm.lect-03.ppt
Yeahhhh the final requirement!!!
Os Reindersfinal
Ad

Recently uploaded (20)

PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Well-logging-methods_new................
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
web development for engineering and engineering
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
additive manufacturing of ss316l using mig welding
PDF
Digital Logic Computer Design lecture notes
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
DOCX
573137875-Attendance-Management-System-original
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Sustainable Sites - Green Building Construction
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Well-logging-methods_new................
Embodied AI: Ushering in the Next Era of Intelligent Systems
web development for engineering and engineering
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Foundation to blockchain - A guide to Blockchain Tech
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
additive manufacturing of ss316l using mig welding
Digital Logic Computer Design lecture notes
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
573137875-Attendance-Management-System-original
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
R24 SURVEYING LAB MANUAL for civil enggi
Mechanical Engineering MATERIALS Selection
Sustainable Sites - Green Building Construction
OOP with Java - Java Introduction (Basics)
UNIT 4 Total Quality Management .pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
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