Difference between revisions of "Team Go"
(Created page with "== Team Go == '''Go Programming Language''' # [mailto:ssaqib1@myseneca.ca?subject=DPS921 Suhaib Saqib] # [mailto:ralmira@myseneca.ca?subject=DPS921 Roana Almira] # [mailt...") |
|||
(22 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== [[Team Go]] == | == [[Team Go]] == | ||
− | + | == Group Members == | |
# [mailto:ssaqib1@myseneca.ca?subject=DPS921 Suhaib Saqib] | # [mailto:ssaqib1@myseneca.ca?subject=DPS921 Suhaib Saqib] | ||
− | |||
# [mailto:lpnewell@myseneca.ca?subject=DPS921 Liam Newell] | # [mailto:lpnewell@myseneca.ca?subject=DPS921 Liam Newell] | ||
− | # [mailto:ssaqib1 | + | # [mailto:ssaqib1@myseneca.ca;lpnewell@myseneca.ca?subject=DPS921 eMail All] |
+ | == Go Programming Language (Golang) == | ||
+ | Go is an open source programming language made by engineers at Google. The project started development in 2007 and was officially announced in 2009. Go had its first stable release on March 16, 2011. Built in the gc compiler Google Go is developed as an open source software. The language targets all three major platforms (Linux, OS X and windows) but can also run on mobile devices. | ||
+ | The Go language originated as an experiment by a few google engineers with the intention to take all the positive characteristics of existing languages and resolve the common criticism. | ||
+ | Go is meant to be statically typed scalable language like C++ or java while also being productive and readable; Go tries to avoid the repetition of too many mandatory keywords | ||
+ | |||
+ | == Understanding the language == | ||
+ | '''Syntax''' | ||
+ | |||
+ | Optional concise variable declaration and initialization | ||
+ | |||
+ | x := 0 not int x = 0 | ||
+ | |||
+ | Remote package management | ||
+ | |||
+ | go get | ||
+ | |||
+ | '''Problem Approaches''' | ||
+ | |||
+ | Built-in concurrency primitives! | ||
+ | |||
+ | Light-weight processes, channels and select statements | ||
+ | |||
+ | Uses interface system instead of virtual inheritance | ||
+ | |||
+ | '''Function returns''' | ||
+ | |||
+ | Functions may return multiple values, and returning a result, err pair is the conventional way a function indicates an error to its caller in Go. | ||
+ | |||
+ | == Concurrency == | ||
+ | |||
+ | making progress on more than one task simultaneously is known as concurrency. | ||
+ | '''Concurrency is not parallelism!''' Concurrency is about ''dealing with'' lots of things at once while parallelism is about ''doing'' lots of things at once. Good concurrency can make it easier to parallelize. | ||
+ | |||
+ | '''Example:''' A computer running mouse and keyboard drivers, network drivers, printer drivers etc. is an example of concurrency. These could all run on a single core and don't have to be parallelized but they can also be run on separate cores to increase throughput. | ||
+ | |||
+ | Go supports concurrency through '''Goroutines''' and '''Channels''' | ||
+ | |||
+ | == Goroutines == | ||
+ | |||
+ | A Goroutine is a function capable of running concurrently with other functions. They're similar to threads except they're much cheaper, light weight and are managed automatically. Goroutines are only a few kilobytes in stack size and their size can grow and shrink whereas thread size needs to be predetermined. Goroutines are multiplexed to fewer OS threads, there may be thousands of Goroutines on on thread. Goroutines communicate using '''channels'''. | ||
+ | <source lang="go"> | ||
+ | package main | ||
+ | |||
+ | import ( | ||
+ | "fmt" | ||
+ | "math/rand" | ||
+ | "time" | ||
+ | ) | ||
+ | |||
+ | func hello(t chan string, num int) { | ||
+ | fmt.Println("Goroutine ", num, " running") | ||
+ | |||
+ | time.Sleep(time.Duration(rand.Intn(5)) * time.Second) | ||
+ | t <- "Hello!" | ||
+ | } | ||
+ | func goaway(g chan string, num int) { | ||
+ | fmt.Println("Goroutine ", num, " running") | ||
+ | time.Sleep(time.Duration(rand.Intn(5)) * time.Second) | ||
+ | g <- "go away!" | ||
+ | } | ||
+ | func main() { | ||
+ | |||
+ | t := make(chan string) | ||
+ | g := make(chan string) | ||
+ | for i := 0; i < 5; i++ { | ||
+ | fmt.Println(i) | ||
+ | if i%2 == 0 { | ||
+ | go hello(t, i) | ||
+ | } else { | ||
+ | go goaway(g, i) | ||
+ | } | ||
+ | } | ||
+ | |||
+ | select { | ||
+ | case word := <-t: | ||
+ | fmt.Println("The compiler said", word) | ||
+ | case word := <-g: | ||
+ | fmt.Println("The Compiler said", word) | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </source> | ||
+ | |||
+ | produces the output: | ||
+ | |||
+ | <source> | ||
+ | 0 | ||
+ | 1 | ||
+ | 2 | ||
+ | 3 | ||
+ | 4 | ||
+ | Goroutine 4 running | ||
+ | Goroutine 0 running | ||
+ | Goroutine 1 running | ||
+ | Goroutine 2 running | ||
+ | Goroutine 3 running | ||
+ | The Compiler said go away! | ||
+ | </source> | ||
+ | |||
+ | Since the ''go'' keyword is used, the program doesn't wait for each function call to complete before moving on which means the order of the Println statements is scrambled. | ||
+ | |||
+ | == Channels == | ||
+ | |||
+ | Channels are what Goroutines use to communicate with each other. They help Goroutines synchronize their execution. Channels can have directions that restrict them to just sending or receiving. Channels can have several parts to them: the type of element you can send through a channel, its capacity or buffer size and the direction of communication which is specified using a <- operator. Data can be sent from one end and received at the other. A communication can be sent using '''t <- argument''' Or received using '''variable := <- t'''. | ||
+ | |||
+ | |||
+ | <source lang ="go"> | ||
+ | package main | ||
+ | |||
+ | import "fmt" | ||
+ | |||
+ | func sum(s []int, c chan int) { | ||
+ | sum := 0 | ||
+ | for _, v := range s { | ||
+ | sum += v | ||
+ | } | ||
+ | c <- sum // send sum to c | ||
+ | } | ||
+ | |||
+ | func main() { | ||
+ | s := []int{7, 2, 8, -9, 4, 0} | ||
+ | |||
+ | c := make(chan int) | ||
+ | go sum(s[:len(s)/2], c) | ||
+ | go sum(s[len(s)/2:], c) | ||
+ | x, y := <-c, <-c // receive from c | ||
+ | |||
+ | fmt.Println(x, y, x+y) | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | Output: | ||
+ | |||
+ | <source> | ||
+ | -5 | ||
+ | 17 | ||
+ | 12 | ||
+ | </source> | ||
+ | |||
+ | == Goroutines vs Threads == | ||
+ | |||
+ | '''C++ Threads''' | ||
+ | |||
+ | Can be Launched concurrently with other such sequences in multithreading environments | ||
+ | |||
+ | Advantages: | ||
+ | |||
+ | * Overall more complex with more customization | ||
+ | |||
+ | * Is joinable | ||
+ | |||
+ | '''Goroutines''' | ||
+ | |||
+ | Google's version of concurrently; contains many of the basic traits of threading | ||
+ | |||
+ | Advantages: | ||
+ | *Goroutines are multiplexed onto the hardware threads meaning they are far more lightweight | ||
+ | |||
+ | *Guaranteed to play nice | ||
+ | |||
+ | == Sources == | ||
+ | |||
+ | [https://www.golang-book.com/books/intro/10 Concurrency] | ||
+ | |||
+ | [https://tour.golang.org/concurrency/1 A Tour of Go] | ||
+ | |||
+ | [https://blog.golang.org/concurrency-is-not-parallelism Concurrency is not Parallelism] | ||
+ | |||
+ | [https://golang.org/pkg/math/rand/ The Go Programming Language] | ||
+ | |||
+ | [http://guzalexander.com/2013/12/06/golang-channels-tutorial.html Golang Channels Tutorial] |
Latest revision as of 17:04, 18 December 2017
Contents
Team Go
Group Members
Go Programming Language (Golang)
Go is an open source programming language made by engineers at Google. The project started development in 2007 and was officially announced in 2009. Go had its first stable release on March 16, 2011. Built in the gc compiler Google Go is developed as an open source software. The language targets all three major platforms (Linux, OS X and windows) but can also run on mobile devices. The Go language originated as an experiment by a few google engineers with the intention to take all the positive characteristics of existing languages and resolve the common criticism. Go is meant to be statically typed scalable language like C++ or java while also being productive and readable; Go tries to avoid the repetition of too many mandatory keywords
Understanding the language
Syntax
Optional concise variable declaration and initialization
x := 0 not int x = 0
Remote package management
go get
Problem Approaches
Built-in concurrency primitives!
Light-weight processes, channels and select statements
Uses interface system instead of virtual inheritance
Function returns
Functions may return multiple values, and returning a result, err pair is the conventional way a function indicates an error to its caller in Go.
Concurrency
making progress on more than one task simultaneously is known as concurrency. Concurrency is not parallelism! Concurrency is about dealing with lots of things at once while parallelism is about doing lots of things at once. Good concurrency can make it easier to parallelize.
Example: A computer running mouse and keyboard drivers, network drivers, printer drivers etc. is an example of concurrency. These could all run on a single core and don't have to be parallelized but they can also be run on separate cores to increase throughput.
Go supports concurrency through Goroutines and Channels
Goroutines
A Goroutine is a function capable of running concurrently with other functions. They're similar to threads except they're much cheaper, light weight and are managed automatically. Goroutines are only a few kilobytes in stack size and their size can grow and shrink whereas thread size needs to be predetermined. Goroutines are multiplexed to fewer OS threads, there may be thousands of Goroutines on on thread. Goroutines communicate using channels.
package main
import (
"fmt"
"math/rand"
"time"
)
func hello(t chan string, num int) {
fmt.Println("Goroutine ", num, " running")
time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
t <- "Hello!"
}
func goaway(g chan string, num int) {
fmt.Println("Goroutine ", num, " running")
time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
g <- "go away!"
}
func main() {
t := make(chan string)
g := make(chan string)
for i := 0; i < 5; i++ {
fmt.Println(i)
if i%2 == 0 {
go hello(t, i)
} else {
go goaway(g, i)
}
}
select {
case word := <-t:
fmt.Println("The compiler said", word)
case word := <-g:
fmt.Println("The Compiler said", word)
}
}
produces the output:
0
1
2
3
4
Goroutine 4 running
Goroutine 0 running
Goroutine 1 running
Goroutine 2 running
Goroutine 3 running
The Compiler said go away!
Since the go keyword is used, the program doesn't wait for each function call to complete before moving on which means the order of the Println statements is scrambled.
Channels
Channels are what Goroutines use to communicate with each other. They help Goroutines synchronize their execution. Channels can have directions that restrict them to just sending or receiving. Channels can have several parts to them: the type of element you can send through a channel, its capacity or buffer size and the direction of communication which is specified using a <- operator. Data can be sent from one end and received at the other. A communication can be sent using t <- argument Or received using variable := <- t.
package main
import "fmt"
func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
c <- sum // send sum to c
}
func main() {
s := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(s[:len(s)/2], c)
go sum(s[len(s)/2:], c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y, x+y)
}
Output:
-5
17
12
Goroutines vs Threads
C++ Threads
Can be Launched concurrently with other such sequences in multithreading environments
Advantages:
- Overall more complex with more customization
- Is joinable
Goroutines
Google's version of concurrently; contains many of the basic traits of threading
Advantages:
- Goroutines are multiplexed onto the hardware threads meaning they are far more lightweight
- Guaranteed to play nice