import "fmt"
n, err := fmt.Println("Hello, Gophers")
Hello, Gophers
"Like C, but sane" Achilleas Koutsou, 2018
...
Most G-Node projects started after 2016 are written in Go: GIN, GIN Client, GIN DOI, GIN Indexing Service, and GIN Validator (if it's GIN, it's probably Go).
Source: Stack Overflow developer survey, 2017 https://insights.stackoverflow.com/survey/2017#technology
// C-like structs
type NewStruct struct {
Prompt string
Number int
}
// Can define methods on structs
func (ns NewStruct) ToString() string {
// C-like print directives and formatting
return fmt.Sprintf("%s: %d", ns.Prompt, ns.Number)
}
import "log"
// Type inference with :=
number := 10 * 42
news := NewStruct{"Number", number}
s := news.ToString()
// Explicit and localised error handling
n, err := fmt.Println(s)
if err != nil {
log.Fatal("Oh no! Print is broken")
}
Number: 420
Not the same, but related.
Concurrency is about structure, parallelism is about execution.
Concurrency provides a way to structure a solution to solve a problem that may (but not necessarily) be parallelizable.
Taken from https://talks.golang.org/2012/waza.slide#8 "Concurrency is not Parallelism"
Taken from https://talks.golang.org/2012/waza.slide#19
import (
"fmt"
"math/rand"
)
func RunSim(name string, steps int) <-chan int {
spikechan := make(chan int)
go func() {
defer close(spikechan)
for t := 0; t < steps; t++ {
if rand.Float32() > 0.8 {
spikechan <- t
}
}
return
}()
return spikechan
}
reflect.Value.Convert: value of type func(reflect.Value) cannot be converted to type func(chan int)
func main() {
a := RunSim("A", 100)
b := RunSim("B", 100)
c := RunSim("C", 100)
for {
select {
case ta, ok := <-a:
if ok {
fmt.Printf("A spiked at %d\n", ta)
} else {
a = nil
}
case tb, ok := <-b:
if ok {
fmt.Printf("B spiked at %d\n", tb)
} else {
b = nil
}
case tc, ok := <-c:
if ok {
fmt.Printf("C spiked at %d\n", tc)
} else {
c = nil
}
}
if a == nil && b == nil && c == nil {
break
}
}
}
repl.go:3:10: undefined identifier: RunSim
Find everything you need at https://github.com/avelino/awesome-go
You can find this presentation at https://gin.g-node.org/G-Node/Go-CNS-talk-2018