Difference between revisions of "Team Go"

From CDOT Wiki
Jump to: navigation, search
(Goroutines)
Line 42: Line 42:
 
== Goroutines ==
 
== 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'''.
+
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">
 
<source lang="go">
 
package main
 
package main
Line 48: Line 48:
 
import (
 
import (
 
"fmt"
 
"fmt"
 +
"math/rand"
 
"time"
 
"time"
 
)
 
)
  
func say(s string) {
+
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++ {
 
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
+
fmt.Println(i)
fmt.Println(s)
+
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)
 
}
 
}
 
}
 
}
  
func main() {
+
</source>
go say("world")
+
 
say("hello")
+
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>
 
</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. Channels help Goroutines synchronize their execution. Channels can have directions that restrict them to just sending or receiving. Channels have several characteristics: the type of element you can send through a channel, capacity (or buffer size) and direction of communication specified by a <- operator.

Revision as of 23:59, 17 December 2017

Team Go

Group Members

  1. Suhaib Saqib
  2. Liam Newell
  3. 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. 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. Channels help Goroutines synchronize their execution. Channels can have directions that restrict them to just sending or receiving. Channels have several characteristics: the type of element you can send through a channel, capacity (or buffer size) and direction of communication specified by a <- operator.