Difference between revisions of "Team Go"
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 and are managed automatically. | + | 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. |
+ | <source lang="go"> | ||
+ | package main | ||
+ | |||
+ | import ( | ||
+ | "fmt" | ||
+ | "time" | ||
+ | ) | ||
+ | |||
+ | func say(s string) { | ||
+ | for i := 0; i < 5; i++ { | ||
+ | time.Sleep(100 * time.Millisecond) | ||
+ | fmt.Println(s) | ||
+ | } | ||
+ | } | ||
+ | |||
+ | func main() { | ||
+ | go say("world") | ||
+ | say("hello") | ||
+ | } | ||
+ | |||
+ | </source> |
Revision as of 22:38, 17 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. 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.
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}