Go Programming Language Lecture Notes PDF
Document Details
Uploaded by BelovedCentaur9471
University of Szeged
Dr. Kertész Attila
Tags
Summary
These are lecture notes on the Go programming language from the University of Szeged. The notes cover various topics such as concurrency and data types. They are valuable for students to learn important Go programming concepts.
Full Transcript
UNIVERSITAS SCIENTIARUM SZEGEDIENSIS SZEGEDI TUDOMÁNYEGYETEM Szoftverfejlesztés Tanszék Concurrent and parallel constructs in Go Dr. Kertész Attila keratt (at) inf.u-szeged.hu https://go.dev/doc/gopher/fiveyears.jpg ...
UNIVERSITAS SCIENTIARUM SZEGEDIENSIS SZEGEDI TUDOMÁNYEGYETEM Szoftverfejlesztés Tanszék Concurrent and parallel constructs in Go Dr. Kertész Attila keratt (at) inf.u-szeged.hu https://go.dev/doc/gopher/fiveyears.jpg History of Go SZEGEDI TUDOMÁNYEGYETEM 1978: Tony Hoare published Communicating Sequential Processes (CSP) with a mathematical foundation, as a Tanszék UNIVERSITAS SCIENTIARUM SZEGEDIENSIS new basis for concurrent programming languages Occam is built upon the CSP model, designed in Szoftverfejlesztés parallel through the 80s by David May in INMOS (UK) for Transputer microprocessors v2.1 published in 1994, latest version of KRoC in 2006 Occam has been used since for safety-critical systems, e.g. in CERN Go: designed by Google from 2007, also built on CSP, published in 2012 Written in Go: Dropbox, Docker, Netflix, Uber 2 Syntax of Go aka Golang (.org) SZEGEDI TUDOMÁNYEGYETEM Similar to C Tanszék UNIVERSITAS SCIENTIARUM SZEGEDIENSIS Statically typed, procedural language Basic types: bool, int, float, byte, string (, pointer-nil) Szoftverfejlesztés var i int var s string = “string” OR s := “string” const Pi = 3.14 var a int OR a := make([]int, 10) array len(a) → 10 fmt.Println(s[1:4]) → “tri” slice 3 Syntax of Go – Control constructs SZEGEDI TUDOMÁNYEGYETEM for: while (for!): Tanszék UNIVERSITAS SCIENTIARUM SZEGEDIENSIS Szoftverfejlesztés package main func main() { import "fmt" (Init statement) sum := 1 Condition exp. for sum < 1000 func main() { (Post st.) { sum := 0 sum += sum } for i := 0; i < 10; i++ { fmt.Println(sum) sum += i } } fmt.Println(sum) } 4 Syntax of Go – Control constructs SZEGEDI TUDOMÁNYEGYETEM (Init statement if: return value type Tanszék before condition) UNIVERSITAS SCIENTIARUM SZEGEDIENSIS function Szoftverfejlesztés func pow(x, n, lim float64) float64 { if v := math.Pow(x, n); v < lim { return v } else { fmt.Printf("%g >= %g\n", v, lim) } // can't use v here, though return lim } 5 Syntax of Go – Control constructs SZEGEDI TUDOMÁNYEGYETEM Tanszék UNIVERSITAS SCIENTIARUM SZEGEDIENSIS switch: Szoftverfejlesztés switch os := runtime.GOOS; os { case "darwin": fmt.Println("OS X.") case "linux": fmt.Println("Linux.") default: // freebsd, openbsd, // plan9, windows... fmt.Printf("%s.\n", os) } 6 Syntax of Go – Control constructs SZEGEDI TUDOMÁNYEGYETEM defer: Tanszék UNIVERSITAS SCIENTIARUM SZEGEDIENSIS - evaluated immediately, but the function call is not executed until the surrounding function returns Szoftverfejlesztés - deferred function calls are pushed onto a stack, and are executed in last-in-first-out package main import "fmt" func main() { defer fmt.Println("world") fmt.Println("hello") } 7 Syntax of Go – data types SZEGEDI TUDOMÁNYEGYETEM type, struct, interface, method in Go: type geometry interface { func (c circle) perim() float64 { area() float64 return 2 * math.Pi * c.radius perim() float64 Tanszék } UNIVERSITAS SCIENTIARUM SZEGEDIENSIS } func measure(g geometry) { type rect struct { Szoftverfejlesztés fmt.Println(g) width, height float64 fmt.Println(g.area()) } fmt.Println(g.perim()) type circle struct { } radius float64 } func main() { r := rect{width: 3, height: 4} func (r rect) area() float64 { c := circle{radius: 5} return r.width * r.height } measure(r) func (r rect) perim() float64 { measure(c) return 2*r.width + 2*r.height } } func (c circle) area() float64 { return math.Pi * c.radius * c.radius } 8 Concurrency in Go SZEGEDI TUDOMÁNYEGYETEM Threads are represented by goroutines in Go Goroutines are lightweight threads, assigned to OS Tanszék UNIVERSITAS SCIENTIARUM SZEGEDIENSIS processes with the Go runtime system Szoftverfejlesztés go: main() func say(s string) { P1 fmt.Println(s) } func main() { P2 say("Hello") // P1 go say("world") // P2 main ends say("!") // P1 } Hello ! 9 Concurrency in Go SZEGEDI TUDOMÁNYEGYETEM main() need to wait for the end of P2 (anonymous function) Can be done with WaitGroups Tanszék UNIVERSITAS SCIENTIARUM SZEGEDIENSIS func main() { var wg sync.WaitGroup Szoftverfejlesztés fmt.Println("hello") // P1 main() wg.Add(1) add one process to WG go func() { // P2 P1 time.Sleep(1) fmt.Println("world") P2 wg.Done() finish one process in WG }() wg.Wait() wait for WG fmt.Println("!") // P1 } Hello main() ends world ! 10 Channels in Go SZEGEDI TUDOMÁNYEGYETEM Go channels are: Typed, bidirectional, and synchronous (by default) Tanszék UNIVERSITAS SCIENTIARUM SZEGEDIENSIS ch := make(chan int) Szoftverfejlesztés ch