SlideShare a Scribd company logo
1 of 16
Download to read offline
go
a crash course
http://golang.org/
a powerful language
low ceremony C-style syntax
garbage collection
static inferred typing
communication-based concurrency
compiled
statically linked
concrete types
package Integer
type Int int
func (i *Int) Add(x int) {
*i += Int(x)
}
type Buffer []Int
func (b Buffer) Clone() Buffer {
s := make(Buffer, len(b))
copy(s, b)
return s
}
func (b Buffer) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
func (b Buffer) Move(i, n int) {
if n > len(b) - i {
n = len(b) - i
}
segment_to_move := b[:i].Clone()
copy(b, b[i:i + n])
copy(b[n:i + n], segment_to_move)
}
package main
import "Integer"
import "fmt"
func main() {
i := Integer.Buffer{0, 1, 2, 3, 4, 5}
b := i.Clone()
b.Swap(1, 2)
b.Move(3, 2)
b[0].Add(3)
fmt.Printf("b[0:2] = %vn", b[0:2])
}
produces:
b[0:2] = [ 6, 4 ]
package Vector
import . "Integer"
type Vector struct {
Buffer
}
func (v *Vector) Clone() Vector {
return Vector{v.Buffer.Clone()}
}
func (v *Vector) Slice(i, j int) Buffer {
return v.Buffer[i:j]
}
func (v *Vector) Replace(o interface{}) {
switch o := o.(type) {
case *Vector:
*v = *o
case Vector:
v = o
case Buffer:
v.Buffer = o
}
}
package main
import "Vector"
import "fmt"
func main() {
i := Vector{Buffer{0, 1, 2, 3, 4, 5}}
v := i.Clone()
v.Swap(1, 2)
v.Move(3, 2)
s := v.Slice(0, 2)
s[0].Add(3)
v.Replace(s)
fmt.Println("b[0:2]", v.Buffer[0:2])
}
produces:
b[0:2] = [ 6, 4 ]
structural types
package main
import "fmt"
type Adder interface {
Add(j int)
Subtract(j int)
Result() interface{}
}
type Calculator struct {
Adder
}
type IntAdder []int
func (i IntAdder) Add(j int) {
i[0] += i[j]
}
func (i IntAdder) Subtract(j int) {
i[0] -= i[j]
}
func (i IntAdder) Result() interface{} {
return i[0]
}
type FloatAdder []float
func (f FloatAdder) Add(j int) {
f[0] += f[j]
}
func (f FloatAdder) Subtract(j int) {
f[0] -= f[j]
}
func (f FloatAdder) Result() interface{} {
return f[0]
}
func main() {
c := Calculator{}
c.Adder = IntAdder{0, 1, 2, 3, 4, 5}
c.Add(1)
c.Add(2)
c.Subtract(3)
fmt.Println(“c.Result() =", c.Result())
c.Adder = FloatAdder{0.0, 1.1, 2.2, 3.3, 4.4, 5.5}
c.Add(1)
c.Add(2)
c.Subtract(3)
fmt.Println("c.Result() =", c.Result())
}
produces:
c.Result() = 0
c.Result() = (0x10f94,0x34800000)
reflected types
package generalise
import "fmt"
import . "reflect"
func Allocate(i interface{}, limit... int) (n interface{}) {
switch v := ValueOf(i); v.Kind() {
case Slice:
l := v.Cap()
if len(limit) > 0 {
l = limit[0]
}
n = MakeSlice(v.Type(), l, l).Interface()
case Map:
n = MakeMap(v.Type()).Interface()
}
return
}
func SwapSlices(i interface{}, d, s, n int) {
if v := ValueOf(i); v.Kind() == Slice {
source := v.Slice(s, s + n)
destination := v.Slice(d, d + n)
temp := ValueOf(Allocate(i, n))
Copy(temp, destination)
Copy(destination, source)
Copy(source, temp)
}
}
func Duplicate(i interface{}) (clone interface{}) {
if clone = Allocate(i); clone != nil {
switch clone := ValueOf(clone); clone.Kind() {
case Slice:
Copy(clone, ValueOf(i))
case Map:
m := ValueOf(i)
for _, k := range m.MapKeys() {
clone.SetMapIndex(k, m.MapIndex(k))
}
}
}
return
}
package main
import . "generalise"
func main() {
error_text := "panic caused by"
defer func() {
if x := recover(); x != nil {
fmt.Println(error_text, x)
}
}()
s1 := []int{0, 1, 2, 3, 4, 5}
fmt.Println("s1 =", s1)
s2 := Duplicate(s1)
fmt.Println("Duplicate(s1) =", s2)
SwapSlices(s2, 0, 3, 3)
fmt.Println("SwapSlices(s2, 0, 3, 3) =", s2)
s3 := Allocate(s1, 1)
fmt.Println("Allocate(s1, 1) =", s3)
m := map[int] int{1: 1, 2: 2, 3: 3, 0: 0, 4: 4, 5: 5}
fmt.Println("m =", m)
n := Allocate(m)
fmt.Println("Allocate(m) =", n)
SwapSlices(m, 0, 3, 3)
}
produces:
s1 = [0 1 2 3 4 5]
Duplicate(s1) = [0 1 2 3 4 5]
SwapSlices(s2, 0, 3, 3) = [3 4 5 0 1 2]
Allocate(s1, 1) = [0]
m = map[3:3 0:0 1:1 4:4 5:5 2:2]
n = map[]
panic caused by map[3:3 0:0 1:1 4:4 5:5 2:2]
concurrency
package generalise
type SignalSource func(status chan bool)
func Pipeline(s… SignalSource) {
done := make(chan bool)
go func() {
defer close(done)
for _, f := range s {
go f(done)
<-done
}
}
<- done
}
package generalise
import . "reflect"
type Iterator func(k, x interface{})
func (i Iterator) apply(k, v interface{}, c chan bool) {
go func() {
i(k, v)
c <- true
}()
}
type Series map[interface{}] interface{}
func (s Series) Apply(f Iterator) {
Pipeline(func(done chan bool) {
for k, v := range s {
f.apply(k, v, done)
}
for i := 0; i < len(s); i++ {
<- done
}
})
}
func Each(c interface{}, f Iterator) (i int) {
s := make(map[interface{}] interface{})
switch c := ValueOf(c); c.Kind() {
case Slice:
for i := 0; i < c.Len(); i++ {
s[i] = c.Index(k).Interface()
}
case Map:
for _, k := range c.Keys() {
s[i] = c.MapIndex(k).Interface()
}
}
return s.Apply(f)
}
package generalise
import . "reflect"
type Results chan interface{}
type Combinator func(x, y interface{}) interface{}
func (f Combinator) Reduce(c, s interface{}) (r Results) {
r = make(Results)
Each(c, func(k, x interface{}) {
s = f(s, x)
})
r <- s
return
}
package generalise
import . "reflect"
type Transformer func(x interface{}) interface{}
func (t Transformer) GetValue(x interface{}) Value {
return ValueOf(t(x))
}
func (t Transformer) Map(c interface{}) (r interface{}) {
switch n := ValueOf(Allocate(c)); n.Kind() {
case Slice:
Each(c, func(k, x interface{}) {
Index(k.(int)).Set(t.GetValue(x))
})
r = n.Interface()
case Map:
Each(c, func(k, x interface{}) {
n.SetElem(NewValue(k), t.GetValue(x))
})
r = n.Interface()
default:
r = Duplicate(c)
}
return
}
package main
import "fmt"
import . "generalise"
var adder Combinator = func(x, y interface{}) interface{} {
return x.(int) + y.(int)
}
var multiplier Transformer = func(x interface{}) interface{} {
return x.(int) * 2
}
func main() {
s := []int{0, 1, 2, 3, 4, 5}
fmt.Println("s =", s)
fmt.Println("sum s =", <- adder.Reduce(s, 0))
c := multiplier.Map(s)
fmt.Println("c =", c)
fmt.Println("sum c =", <- adder.Reduce(c, 0))
}
produces:
s = [0 1 2 3 4 5]
sum s = 15
c = [0 2 4 6 8 10]
sum c = 30

More Related Content

What's hot

All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2goMoriyoshi Koizumi
 
Implementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxImplementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxEleanor McHugh
 
The Browser Environment - A Systems Programmer's Perspective
The Browser Environment - A Systems Programmer's PerspectiveThe Browser Environment - A Systems Programmer's Perspective
The Browser Environment - A Systems Programmer's PerspectiveEleanor McHugh
 
Diving into byte code optimization in python
Diving into byte code optimization in python Diving into byte code optimization in python
Diving into byte code optimization in python Chetan Giridhar
 
.NET 2015: Будущее рядом
.NET 2015: Будущее рядом.NET 2015: Будущее рядом
.NET 2015: Будущее рядомAndrey Akinshin
 
Go: It's Not Just For Google
Go: It's Not Just For GoogleGo: It's Not Just For Google
Go: It's Not Just For GoogleEleanor McHugh
 
4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладкаDEVTYPE
 
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonakaptur
 
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...akaptur
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Andrey Akinshin
 
Python opcodes
Python opcodesPython opcodes
Python opcodesalexgolec
 

What's hot (20)

All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
 
Implementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxImplementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 redux
 
C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
 
C++ TUTORIAL 7
C++ TUTORIAL 7C++ TUTORIAL 7
C++ TUTORIAL 7
 
The Browser Environment - A Systems Programmer's Perspective
The Browser Environment - A Systems Programmer's PerspectiveThe Browser Environment - A Systems Programmer's Perspective
The Browser Environment - A Systems Programmer's Perspective
 
Diving into byte code optimization in python
Diving into byte code optimization in python Diving into byte code optimization in python
Diving into byte code optimization in python
 
C++ TUTORIAL 9
C++ TUTORIAL 9C++ TUTORIAL 9
C++ TUTORIAL 9
 
.NET 2015: Будущее рядом
.NET 2015: Будущее рядом.NET 2015: Будущее рядом
.NET 2015: Будущее рядом
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
Go: It's Not Just For Google
Go: It's Not Just For GoogleGo: It's Not Just For Google
Go: It's Not Just For Google
 
C++ TUTORIAL 10
C++ TUTORIAL 10C++ TUTORIAL 10
C++ TUTORIAL 10
 
4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка
 
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
 
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
 
Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?Что нам готовит грядущий C#7?
Что нам готовит грядущий C#7?
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
 
Python opcodes
Python opcodesPython opcodes
Python opcodes
 

Similar to Go a crash course

GoLightly: Building VM-Based Language Runtimes with Google Go
GoLightly: Building VM-Based Language Runtimes with Google GoGoLightly: Building VM-Based Language Runtimes with Google Go
GoLightly: Building VM-Based Language Runtimes with Google GoEleanor McHugh
 
Google Go For Ruby Hackers
Google Go For Ruby HackersGoogle Go For Ruby Hackers
Google Go For Ruby HackersEleanor McHugh
 
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.Scala-Gopher: CSP-style programming techniques with idiomatic Scala.
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.Ruslan Shevchenko
 
Go ahead, make my day
Go ahead, make my dayGo ahead, make my day
Go ahead, make my dayTor Ivry
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patternsleague
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)Ishin Vin
 
3 kotlin vs. java- what kotlin has that java does not
3  kotlin vs. java- what kotlin has that java does not3  kotlin vs. java- what kotlin has that java does not
3 kotlin vs. java- what kotlin has that java does notSergey Bandysik
 
Introduction to go
Introduction to goIntroduction to go
Introduction to goJaehue Jang
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptxGuy Komari
 
Concurrent Application Development using Scala
Concurrent Application Development using ScalaConcurrent Application Development using Scala
Concurrent Application Development using ScalaSiarhiej Siemianchuk
 
Embedding Generic Monadic Transformer into Scala. [Tfp2022]
Embedding Generic Monadic Transformer into Scala. [Tfp2022]Embedding Generic Monadic Transformer into Scala. [Tfp2022]
Embedding Generic Monadic Transformer into Scala. [Tfp2022]Ruslan Shevchenko
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systemsleague
 
Lego: A brick system build by scala
Lego: A brick system build by scalaLego: A brick system build by scala
Lego: A brick system build by scalalunfu zhong
 
The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)Eric Torreborre
 
Scalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaScalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaDaniel Sebban
 
The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator PatternEric Torreborre
 
Something about Golang
Something about GolangSomething about Golang
Something about GolangAnton Arhipov
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaN Masahiro
 

Similar to Go a crash course (20)

GoLightly: Building VM-Based Language Runtimes with Google Go
GoLightly: Building VM-Based Language Runtimes with Google GoGoLightly: Building VM-Based Language Runtimes with Google Go
GoLightly: Building VM-Based Language Runtimes with Google Go
 
Google Go For Ruby Hackers
Google Go For Ruby HackersGoogle Go For Ruby Hackers
Google Go For Ruby Hackers
 
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.Scala-Gopher: CSP-style programming techniques with idiomatic Scala.
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.
 
Go ahead, make my day
Go ahead, make my dayGo ahead, make my day
Go ahead, make my day
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
 
Monadologie
MonadologieMonadologie
Monadologie
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)
 
3 kotlin vs. java- what kotlin has that java does not
3  kotlin vs. java- what kotlin has that java does not3  kotlin vs. java- what kotlin has that java does not
3 kotlin vs. java- what kotlin has that java does not
 
Introduction to go
Introduction to goIntroduction to go
Introduction to go
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 
Concurrent Application Development using Scala
Concurrent Application Development using ScalaConcurrent Application Development using Scala
Concurrent Application Development using Scala
 
Embedding Generic Monadic Transformer into Scala. [Tfp2022]
Embedding Generic Monadic Transformer into Scala. [Tfp2022]Embedding Generic Monadic Transformer into Scala. [Tfp2022]
Embedding Generic Monadic Transformer into Scala. [Tfp2022]
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systems
 
C++ programs
C++ programsC++ programs
C++ programs
 
Lego: A brick system build by scala
Lego: A brick system build by scalaLego: A brick system build by scala
Lego: A brick system build by scala
 
The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)
 
Scalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaScalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with Scala
 
The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator Pattern
 
Something about Golang
Something about GolangSomething about Golang
Something about Golang
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoya
 

More from Eleanor McHugh

[2023] Putting the R! in R&D.pdf
[2023] Putting the R! in R&D.pdf[2023] Putting the R! in R&D.pdf
[2023] Putting the R! in R&D.pdfEleanor McHugh
 
Generics, Reflection, and Efficient Collections
Generics, Reflection, and Efficient CollectionsGenerics, Reflection, and Efficient Collections
Generics, Reflection, and Efficient CollectionsEleanor McHugh
 
The Relevance of Liveness - Biometrics and Data Integrity
The Relevance of Liveness - Biometrics and Data IntegrityThe Relevance of Liveness - Biometrics and Data Integrity
The Relevance of Liveness - Biometrics and Data IntegrityEleanor McHugh
 
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]Eleanor McHugh
 
Go for the paranoid network programmer, 3rd edition
Go for the paranoid network programmer, 3rd editionGo for the paranoid network programmer, 3rd edition
Go for the paranoid network programmer, 3rd editionEleanor McHugh
 
An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]Eleanor McHugh
 
An introduction to functional programming with go
An introduction to functional programming with goAn introduction to functional programming with go
An introduction to functional programming with goEleanor McHugh
 
Identity & trust in Monitored Spaces
Identity & trust in Monitored SpacesIdentity & trust in Monitored Spaces
Identity & trust in Monitored SpacesEleanor McHugh
 
Don't Ask, Don't Tell - The Virtues of Privacy By Design
Don't Ask, Don't Tell - The Virtues of Privacy By DesignDon't Ask, Don't Tell - The Virtues of Privacy By Design
Don't Ask, Don't Tell - The Virtues of Privacy By DesignEleanor McHugh
 
Don't ask, don't tell the virtues of privacy by design
Don't ask, don't tell   the virtues of privacy by designDon't ask, don't tell   the virtues of privacy by design
Don't ask, don't tell the virtues of privacy by designEleanor McHugh
 
Anonymity, identity, trust
Anonymity, identity, trustAnonymity, identity, trust
Anonymity, identity, trustEleanor McHugh
 
Going Loopy - Adventures in Iteration with Google Go
Going Loopy - Adventures in Iteration with Google GoGoing Loopy - Adventures in Iteration with Google Go
Going Loopy - Adventures in Iteration with Google GoEleanor McHugh
 
Distributed Ledgers: Anonymity & Immutability at Scale
Distributed Ledgers: Anonymity & Immutability at ScaleDistributed Ledgers: Anonymity & Immutability at Scale
Distributed Ledgers: Anonymity & Immutability at ScaleEleanor McHugh
 
Go for the paranoid network programmer, 2nd edition
Go for the paranoid network programmer, 2nd editionGo for the paranoid network programmer, 2nd edition
Go for the paranoid network programmer, 2nd editionEleanor McHugh
 
Going Loopy: Adventures in Iteration with Go
Going Loopy: Adventures in Iteration with GoGoing Loopy: Adventures in Iteration with Go
Going Loopy: Adventures in Iteration with GoEleanor McHugh
 
Finding a useful outlet for my many Adventures in go
Finding a useful outlet for my many Adventures in goFinding a useful outlet for my many Adventures in go
Finding a useful outlet for my many Adventures in goEleanor McHugh
 
Anonymity, trust, accountability
Anonymity, trust, accountabilityAnonymity, trust, accountability
Anonymity, trust, accountabilityEleanor McHugh
 
Implementing Virtual Machines in Go & C
Implementing Virtual Machines in Go & CImplementing Virtual Machines in Go & C
Implementing Virtual Machines in Go & CEleanor McHugh
 
Implementing Virtual Machines in Ruby & C
Implementing Virtual Machines in Ruby & CImplementing Virtual Machines in Ruby & C
Implementing Virtual Machines in Ruby & CEleanor McHugh
 

More from Eleanor McHugh (20)

[2023] Putting the R! in R&D.pdf
[2023] Putting the R! in R&D.pdf[2023] Putting the R! in R&D.pdf
[2023] Putting the R! in R&D.pdf
 
Generics, Reflection, and Efficient Collections
Generics, Reflection, and Efficient CollectionsGenerics, Reflection, and Efficient Collections
Generics, Reflection, and Efficient Collections
 
The Relevance of Liveness - Biometrics and Data Integrity
The Relevance of Liveness - Biometrics and Data IntegrityThe Relevance of Liveness - Biometrics and Data Integrity
The Relevance of Liveness - Biometrics and Data Integrity
 
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
 
Go for the paranoid network programmer, 3rd edition
Go for the paranoid network programmer, 3rd editionGo for the paranoid network programmer, 3rd edition
Go for the paranoid network programmer, 3rd edition
 
An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]
 
An introduction to functional programming with go
An introduction to functional programming with goAn introduction to functional programming with go
An introduction to functional programming with go
 
Identity & trust in Monitored Spaces
Identity & trust in Monitored SpacesIdentity & trust in Monitored Spaces
Identity & trust in Monitored Spaces
 
Don't Ask, Don't Tell - The Virtues of Privacy By Design
Don't Ask, Don't Tell - The Virtues of Privacy By DesignDon't Ask, Don't Tell - The Virtues of Privacy By Design
Don't Ask, Don't Tell - The Virtues of Privacy By Design
 
Don't ask, don't tell the virtues of privacy by design
Don't ask, don't tell   the virtues of privacy by designDon't ask, don't tell   the virtues of privacy by design
Don't ask, don't tell the virtues of privacy by design
 
Anonymity, identity, trust
Anonymity, identity, trustAnonymity, identity, trust
Anonymity, identity, trust
 
Going Loopy - Adventures in Iteration with Google Go
Going Loopy - Adventures in Iteration with Google GoGoing Loopy - Adventures in Iteration with Google Go
Going Loopy - Adventures in Iteration with Google Go
 
Distributed Ledgers: Anonymity & Immutability at Scale
Distributed Ledgers: Anonymity & Immutability at ScaleDistributed Ledgers: Anonymity & Immutability at Scale
Distributed Ledgers: Anonymity & Immutability at Scale
 
Hello Go
Hello GoHello Go
Hello Go
 
Go for the paranoid network programmer, 2nd edition
Go for the paranoid network programmer, 2nd editionGo for the paranoid network programmer, 2nd edition
Go for the paranoid network programmer, 2nd edition
 
Going Loopy: Adventures in Iteration with Go
Going Loopy: Adventures in Iteration with GoGoing Loopy: Adventures in Iteration with Go
Going Loopy: Adventures in Iteration with Go
 
Finding a useful outlet for my many Adventures in go
Finding a useful outlet for my many Adventures in goFinding a useful outlet for my many Adventures in go
Finding a useful outlet for my many Adventures in go
 
Anonymity, trust, accountability
Anonymity, trust, accountabilityAnonymity, trust, accountability
Anonymity, trust, accountability
 
Implementing Virtual Machines in Go & C
Implementing Virtual Machines in Go & CImplementing Virtual Machines in Go & C
Implementing Virtual Machines in Go & C
 
Implementing Virtual Machines in Ruby & C
Implementing Virtual Machines in Ruby & CImplementing Virtual Machines in Ruby & C
Implementing Virtual Machines in Ruby & C
 

Recently uploaded

Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 

Recently uploaded (20)

Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 

Go a crash course

  • 2. a powerful language low ceremony C-style syntax garbage collection static inferred typing communication-based concurrency compiled statically linked
  • 4. package Integer type Int int func (i *Int) Add(x int) { *i += Int(x) } type Buffer []Int func (b Buffer) Clone() Buffer { s := make(Buffer, len(b)) copy(s, b) return s } func (b Buffer) Swap(i, j int) { b[i], b[j] = b[j], b[i] } func (b Buffer) Move(i, n int) { if n > len(b) - i { n = len(b) - i } segment_to_move := b[:i].Clone() copy(b, b[i:i + n]) copy(b[n:i + n], segment_to_move) } package main import "Integer" import "fmt" func main() { i := Integer.Buffer{0, 1, 2, 3, 4, 5} b := i.Clone() b.Swap(1, 2) b.Move(3, 2) b[0].Add(3) fmt.Printf("b[0:2] = %vn", b[0:2]) } produces: b[0:2] = [ 6, 4 ]
  • 5. package Vector import . "Integer" type Vector struct { Buffer } func (v *Vector) Clone() Vector { return Vector{v.Buffer.Clone()} } func (v *Vector) Slice(i, j int) Buffer { return v.Buffer[i:j] } func (v *Vector) Replace(o interface{}) { switch o := o.(type) { case *Vector: *v = *o case Vector: v = o case Buffer: v.Buffer = o } } package main import "Vector" import "fmt" func main() { i := Vector{Buffer{0, 1, 2, 3, 4, 5}} v := i.Clone() v.Swap(1, 2) v.Move(3, 2) s := v.Slice(0, 2) s[0].Add(3) v.Replace(s) fmt.Println("b[0:2]", v.Buffer[0:2]) } produces: b[0:2] = [ 6, 4 ]
  • 7. package main import "fmt" type Adder interface { Add(j int) Subtract(j int) Result() interface{} } type Calculator struct { Adder } type IntAdder []int func (i IntAdder) Add(j int) { i[0] += i[j] } func (i IntAdder) Subtract(j int) { i[0] -= i[j] } func (i IntAdder) Result() interface{} { return i[0] } type FloatAdder []float func (f FloatAdder) Add(j int) { f[0] += f[j] } func (f FloatAdder) Subtract(j int) { f[0] -= f[j] } func (f FloatAdder) Result() interface{} { return f[0] } func main() { c := Calculator{} c.Adder = IntAdder{0, 1, 2, 3, 4, 5} c.Add(1) c.Add(2) c.Subtract(3) fmt.Println(“c.Result() =", c.Result()) c.Adder = FloatAdder{0.0, 1.1, 2.2, 3.3, 4.4, 5.5} c.Add(1) c.Add(2) c.Subtract(3) fmt.Println("c.Result() =", c.Result()) } produces: c.Result() = 0 c.Result() = (0x10f94,0x34800000)
  • 9. package generalise import "fmt" import . "reflect" func Allocate(i interface{}, limit... int) (n interface{}) { switch v := ValueOf(i); v.Kind() { case Slice: l := v.Cap() if len(limit) > 0 { l = limit[0] } n = MakeSlice(v.Type(), l, l).Interface() case Map: n = MakeMap(v.Type()).Interface() } return } func SwapSlices(i interface{}, d, s, n int) { if v := ValueOf(i); v.Kind() == Slice { source := v.Slice(s, s + n) destination := v.Slice(d, d + n) temp := ValueOf(Allocate(i, n)) Copy(temp, destination) Copy(destination, source) Copy(source, temp) } } func Duplicate(i interface{}) (clone interface{}) { if clone = Allocate(i); clone != nil { switch clone := ValueOf(clone); clone.Kind() { case Slice: Copy(clone, ValueOf(i)) case Map: m := ValueOf(i) for _, k := range m.MapKeys() { clone.SetMapIndex(k, m.MapIndex(k)) } } } return }
  • 10. package main import . "generalise" func main() { error_text := "panic caused by" defer func() { if x := recover(); x != nil { fmt.Println(error_text, x) } }() s1 := []int{0, 1, 2, 3, 4, 5} fmt.Println("s1 =", s1) s2 := Duplicate(s1) fmt.Println("Duplicate(s1) =", s2) SwapSlices(s2, 0, 3, 3) fmt.Println("SwapSlices(s2, 0, 3, 3) =", s2) s3 := Allocate(s1, 1) fmt.Println("Allocate(s1, 1) =", s3) m := map[int] int{1: 1, 2: 2, 3: 3, 0: 0, 4: 4, 5: 5} fmt.Println("m =", m) n := Allocate(m) fmt.Println("Allocate(m) =", n) SwapSlices(m, 0, 3, 3) } produces: s1 = [0 1 2 3 4 5] Duplicate(s1) = [0 1 2 3 4 5] SwapSlices(s2, 0, 3, 3) = [3 4 5 0 1 2] Allocate(s1, 1) = [0] m = map[3:3 0:0 1:1 4:4 5:5 2:2] n = map[] panic caused by map[3:3 0:0 1:1 4:4 5:5 2:2]
  • 12. package generalise type SignalSource func(status chan bool) func Pipeline(s… SignalSource) { done := make(chan bool) go func() { defer close(done) for _, f := range s { go f(done) <-done } } <- done }
  • 13. package generalise import . "reflect" type Iterator func(k, x interface{}) func (i Iterator) apply(k, v interface{}, c chan bool) { go func() { i(k, v) c <- true }() } type Series map[interface{}] interface{} func (s Series) Apply(f Iterator) { Pipeline(func(done chan bool) { for k, v := range s { f.apply(k, v, done) } for i := 0; i < len(s); i++ { <- done } }) } func Each(c interface{}, f Iterator) (i int) { s := make(map[interface{}] interface{}) switch c := ValueOf(c); c.Kind() { case Slice: for i := 0; i < c.Len(); i++ { s[i] = c.Index(k).Interface() } case Map: for _, k := range c.Keys() { s[i] = c.MapIndex(k).Interface() } } return s.Apply(f) }
  • 14. package generalise import . "reflect" type Results chan interface{} type Combinator func(x, y interface{}) interface{} func (f Combinator) Reduce(c, s interface{}) (r Results) { r = make(Results) Each(c, func(k, x interface{}) { s = f(s, x) }) r <- s return }
  • 15. package generalise import . "reflect" type Transformer func(x interface{}) interface{} func (t Transformer) GetValue(x interface{}) Value { return ValueOf(t(x)) } func (t Transformer) Map(c interface{}) (r interface{}) { switch n := ValueOf(Allocate(c)); n.Kind() { case Slice: Each(c, func(k, x interface{}) { Index(k.(int)).Set(t.GetValue(x)) }) r = n.Interface() case Map: Each(c, func(k, x interface{}) { n.SetElem(NewValue(k), t.GetValue(x)) }) r = n.Interface() default: r = Duplicate(c) } return }
  • 16. package main import "fmt" import . "generalise" var adder Combinator = func(x, y interface{}) interface{} { return x.(int) + y.(int) } var multiplier Transformer = func(x interface{}) interface{} { return x.(int) * 2 } func main() { s := []int{0, 1, 2, 3, 4, 5} fmt.Println("s =", s) fmt.Println("sum s =", <- adder.Reduce(s, 0)) c := multiplier.Map(s) fmt.Println("c =", c) fmt.Println("sum c =", <- adder.Reduce(c, 0)) } produces: s = [0 1 2 3 4 5] sum s = 15 c = [0 2 4 6 8 10] sum c = 30