SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
Go Containers
January 23, 2014 

John Graham-Cumming

www.cloudflare.com!
Six interesting containers
•  From pkg/container!
•  container/list!
•  container/ring!
•  container/heap

!
•  Built in
•  map!
•  slice
•  Channels as queues

www.cloudflare.com!
container/list!
•  Doubly-linked list implementation
•  Uses interface{} for values
l := list.New()!
e0 := l.PushBack(42)!
e1 := l.PushFront(13)!
Pity	
  there’s	
  no	
  ‘map’	
  
e2 := l.PushBack(7)!
func4on	
  
l.InsertBefore(3, e0)!
l.InsertAfter(196, e1)!
l.InsertAfter(1729, e2)!
!
for e := l.Front(); e != nil; e = e.Next() {!
fmt.Printf("%d ", e.Value.(int))!
}!
e.Value	
  to	
  get	
  the	
  
fmt.Printf("n")!
stored	
  value	
  
!
!
13 196 3 42 7 1729!
www.cloudflare.com!
container/list!

All	
  work	
  on	
  
elements	
  not	
  values	
  	
  

l.MoveToFront(e2)!
l.MoveToBack(e1)!
Returns	
  the	
  value	
  
l.Remove(e0)!
removed	
  
!
for e := l.Front(); e != nil; e = e.Next() {!
fmt.Printf("%d ", e.Value.(int))!
}!
fmt.Printf("n")!
!
!
7 196 3 1729 13!

www.cloudflare.com!
container/ring!
•  A circular ‘list’
parus := []string{”major", “holsti”, “carpi”}!
!
r := ring.New(len(parus))!
for i := 0; i < r.Len(); i++ {!
r.Value = parus[i]!
r = r.Next()!
}!
!
r.Do(func(x interface{}) {!
fmt.Printf(“Parus %sn”, x.(string))!
})!

•  Move n elements through ring
r.Move(n)!

www.cloudflare.com!
container/heap!
•  Implements a “min-heap” (i.e. tree where each node is

the “minimum” element in its subtree)

•  Needs a notion of “Less” and a way to “Swap”
www.cloudflare.com!
container/heap!
•  The single most confusing definition in all of Go
type Interface interface {!
sort.Interface!
Push(x interface{}) // add x as element Len()!
Pop() interface{}
// remove/return element Len()-1!
}!
!
// Note that Push and Pop in this interface are for !
// package heap's implementation to call. To add and !
// remove things from the heap, use heap.Push and!
// heap.Pop.!

www.cloudflare.com!
container/heap!
•  Simple example
type OrderedInts []int!
!
func (h OrderedInts) Len() int { return len(h) }!
func (h OrderedInts) Less(i, j int) bool {!
return h[i] < h[j]!
}!
func (h OrderedInts) Swap(i,j int) {h[i],h[j]=h[j],h[i]}!
func (h *OrderedInts) Push(x interface{}) {!
!*h = append(*h, x.(int))!
}!
func (h *OrderedInts) Pop() interface{} {!
!old := *h!
!n := len(old)-1!
!x := old[n]!
!*h = old[:n]!
!return x!
}!
www.cloudflare.com!
container/heap!
•  Using a heap
h := &OrderedInts{33,76,55,24,48,63,86,83,83,12}!
!
heap.Init(h)!
!
fmt.Printf("min: %dn", (*h)[0])!
!
for h.Len() > 0 {!
fmt.Printf("%d ", heap.Pop(h))!
}!
!
fmt.Printf(“n”)!

www.cloudflare.com!
container/heap!
•  Heaps are useful for...
•  Make a priority queue
•  Sorting
•  Graph algorithms

www.cloudflare.com!
MAP

www.cloudflare.com!
map!
•  Maps are typed



dictionary := make(map[string]string)!
dictionary := map[string]string{}!
!

•  They are not concurrency safe
•  Use a lock or channel for concurrent read/write access
counts := struct{!
sync.RWMutex!
m map[string]int!
}{m: make(map[string]int)}!
!
counts.RLock()!
fmt.Printf(“foo count”, counts.m[“foo”]!
counts.RUnlock()!
!
counts.Lock()!
counts.m[“foo”] += num_foos!
counts.Unlock()!
!
www.cloudflare.com!

Mul4ple	
  readers,	
  
one	
  writer	
  
map iteration
m := map[string]int{!
"bar”: 54,!
"foo”: 42,!
"baz”: -1,!
}!
!
for k := range m {!
// k is foo, bar, baz!
}!
!
for _, v := range m {!
// v is 54, 42, -1 in some order!
}!
!
for k, v := range m {!
// k and v are as above!
}!

www.cloudflare.com!

Order	
  of	
  itera4on	
  is	
  
undefined	
  	
  
Common map operations
•  Remove an element
delete(dictionary, “twerking”)!

•  Test presence of an element
definition, present := dictionary[“hoopy”]!
!
_, present := dictionary[“sigil”]!

•  Missing element gives a “zero” value




fmt.Printf(“[%s]n”, dictionary[“ewyfgwyegfweygf”])!
!
[]!

www.cloudflare.com!
SLICE

www.cloudflare.com!
Slices
•  A slice is part of an array
var arrayOfInts [256]int!
!
var part []int = arrayOfInts[2:6]!

•  arrayOfInts is 256 ints contiguously in memory
0	
  

1	
  

2	
  

3	
  

4	
  

5	
  

6	
  

7	
  

8	
  

9	
  

10	
  

11	
  

12	
  

!
!
•  part consists of a pointer (to arrayOfInts[2]) and a
length (4)

www.cloudflare.com!
Slice passing
•  A slice is passed (like everything else) by copy
var arrayOfInts [256]int!
!
var part []int = arrayOfInts[2:6]!
!
func fill(s []int) {!
for i, _ := range s {!
s[i] = i*2!
}!
!
s = s[1:]!
}!
Does	
  nothing	
  to	
  
!
part!
fill(part)!
fmt.Printf(“%#v”, part)!
!
% ./slice!
[]int{0, 2, 4, 6}!
www.cloudflare.com!

Contents	
  of	
  s	
  can	
  be	
  
modified	
  

Changes	
  contents	
  of	
  
underlying	
  array	
  
Slice passing, part 2
•  Can pass a pointer to a slice to modify the slice
var arrayOfInts [256]int!
!
var part intSlice = arrayOfInts[2:6] !
!
Contents	
  of	
  s	
  can	
  be	
  
type intSlice []int!
modified	
  and	
  s	
  can	
  
func (s *intSlice) fill() {!
be	
  changed	
  
for i, _ := range *s {!
(*s)[i] = i*2!
}!
*s = (*s)[1:]!
Changes	
  part!
}!
!
part.fill()!
fmt.Printf("%#vn", part)!
!
% ./slice!
[]int{2, 4, 6}!
www.cloudflare.com!
Slice iteration
prime := []int{2, 3, 5, 7, 11}!
!
for i := range prime {!
// i is 0, 1, 2, 3, 4!
}!
!
for _, e := range prime{!
// e is 2, 3, 5, 7, 11!
}!
!
for i, e := range prime {!
// i and e as above!
}!

www.cloudflare.com!
Copying slices
•  copy builtin
morePrimes := make([]int, len(primes), 2*cap(primes))!
!
copy(morePrimes, primes)!

•  copy allows source and destination to overlap
primes := [10]int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29}!
odds := primes[1:7]!
!
odds = odds[0:len(odds)+1]!
copy(odds[4:], odds[3:])!
odds[3] = 9!
fmt.Printf("%#vn", odds)!
!
[]int{3, 5, 7, 9, 11, 13, 17}!

www.cloudflare.com!
Appending slices
s := []int{1, 3, 6, 10}!
t := []int{36, 45, 55, 66, 78}!
!
s = append(s, 15)!
Adding	
  individual	
  
elements	
  
s = append(s, 21, 28)!
!
s = append(s, t...)!
!
Adding	
  an	
  en4re	
  
nu := append([]int(nil), s...)!
slice	
  
!
s = append(s, s...)!
Copying	
  a	
  slice	
  (use	
  
!
copy	
  instead)	
  
fmt.Printf(“%#vn”, s)!
!
[]int{1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 1, 3,
6, 10, 15, 21, 28, 36, 45, 55, 66, 78}!

www.cloudflare.com!
CHANNELS AS QUEUES

www.cloudflare.com!
A buffered channel is a FIFO queue 
•  A typed queue of up to 10 Things



queue := make(chan Thing, 10)

!

•  Get the next element from the queue if there is one



select {!
case t := <-queue: // got one!
default:
// queue is empty!
}
!

•  Add to queue if there’s room
select {!
case queue <- t: // added to queue!
default:
// queue is full!
}!

www.cloudflare.com!
GENERICS

www.cloudflare.com!
Perhaps heretical
•  But... I wish Go had some generics
•  interface{} is like void *; Type assertions similar to casts
l := list.New()!
l.PushFront("Hello, World!")!
v := l.Front()!
i := v.Value.(int)!
% go build l.go!
% ./l!
panic: interface conversion: interface is
string, not int!
!
goroutine 1 [running]:!
runtime.panic(0x49bdc0, 0xc210041000)!
!/extra/go/src/pkg/runtime/panic.c:266
+0xb6!
main.main()!
!/extra/src/mc/generic.go:12 +0xaa!
www.cloudflare.com!
Sources etc.
•  Slides and code samples for this talk:

https://github.com/cloudflare/jgc-talks/tree/master/
Go_London_User_Group/Go_Containers

•  All my talks (with data/code) on the CloudFlare Github

https://github.com/cloudflare/jgc-talks
•  All my talks on the CloudFlare SlideShare

http://www.slideshare.net/cloudflare

www.cloudflare.com!

Más contenido relacionado

La actualidad más candente

Go Concurrency
Go ConcurrencyGo Concurrency
Go ConcurrencyCloudflare
 
Rubinius @ RubyAndRails2010
Rubinius @ RubyAndRails2010Rubinius @ RubyAndRails2010
Rubinius @ RubyAndRails2010Dirkjan Bussink
 
An Introduction to Go
An Introduction to GoAn Introduction to Go
An Introduction to GoCloudflare
 
Something about Golang
Something about GolangSomething about Golang
Something about GolangAnton Arhipov
 
PyCon KR 2019 sprint - RustPython by example
PyCon KR 2019 sprint  - RustPython by examplePyCon KR 2019 sprint  - RustPython by example
PyCon KR 2019 sprint - RustPython by exampleYunWon Jeong
 
Python Coroutines, Present and Future
Python Coroutines, Present and FuturePython Coroutines, Present and Future
Python Coroutines, Present and Futureemptysquare
 
All you need to know about the JavaScript event loop
All you need to know about the JavaScript event loopAll you need to know about the JavaScript event loop
All you need to know about the JavaScript event loopSaša Tatar
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutinesNAVER Engineering
 
Naughty And Nice Bash Features
Naughty And Nice Bash FeaturesNaughty And Nice Bash Features
Naughty And Nice Bash FeaturesNati Cohen
 
When RegEx is not enough
When RegEx is not enoughWhen RegEx is not enough
When RegEx is not enoughNati Cohen
 
Happy Go Programming
Happy Go ProgrammingHappy Go Programming
Happy Go ProgrammingLin Yo-An
 
Python meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/OPython meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/OBuzzcapture
 
PyCon lightning talk on my Toro module for Tornado
PyCon lightning talk on my Toro module for TornadoPyCon lightning talk on my Toro module for Tornado
PyCon lightning talk on my Toro module for Tornadoemptysquare
 
Kotlin Coroutines. Flow is coming
Kotlin Coroutines. Flow is comingKotlin Coroutines. Flow is coming
Kotlin Coroutines. Flow is comingKirill Rozov
 

La actualidad más candente (20)

Go Concurrency
Go ConcurrencyGo Concurrency
Go Concurrency
 
node ffi
node ffinode ffi
node ffi
 
Rubinius @ RubyAndRails2010
Rubinius @ RubyAndRails2010Rubinius @ RubyAndRails2010
Rubinius @ RubyAndRails2010
 
An Introduction to Go
An Introduction to GoAn Introduction to Go
An Introduction to Go
 
Something about Golang
Something about GolangSomething about Golang
Something about Golang
 
PyCon KR 2019 sprint - RustPython by example
PyCon KR 2019 sprint  - RustPython by examplePyCon KR 2019 sprint  - RustPython by example
PyCon KR 2019 sprint - RustPython by example
 
Rcpp11 genentech
Rcpp11 genentechRcpp11 genentech
Rcpp11 genentech
 
Python Coroutines, Present and Future
Python Coroutines, Present and FuturePython Coroutines, Present and Future
Python Coroutines, Present and Future
 
All you need to know about the JavaScript event loop
All you need to know about the JavaScript event loopAll you need to know about the JavaScript event loop
All you need to know about the JavaScript event loop
 
R/C++ talk at earl 2014
R/C++ talk at earl 2014R/C++ talk at earl 2014
R/C++ talk at earl 2014
 
Docopt
DocoptDocopt
Docopt
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutines
 
Naughty And Nice Bash Features
Naughty And Nice Bash FeaturesNaughty And Nice Bash Features
Naughty And Nice Bash Features
 
When RegEx is not enough
When RegEx is not enoughWhen RegEx is not enough
When RegEx is not enough
 
Happy Go Programming
Happy Go ProgrammingHappy Go Programming
Happy Go Programming
 
Python meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/OPython meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/O
 
Python Async IO Horizon
Python Async IO HorizonPython Async IO Horizon
Python Async IO Horizon
 
PyCon lightning talk on my Toro module for Tornado
PyCon lightning talk on my Toro module for TornadoPyCon lightning talk on my Toro module for Tornado
PyCon lightning talk on my Toro module for Tornado
 
2015 555 kharchenko_ppt
2015 555 kharchenko_ppt2015 555 kharchenko_ppt
2015 555 kharchenko_ppt
 
Kotlin Coroutines. Flow is coming
Kotlin Coroutines. Flow is comingKotlin Coroutines. Flow is coming
Kotlin Coroutines. Flow is coming
 

Similar a Go Containers

JVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in KotlinJVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in KotlinAndrey Breslav
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?Adam Dudczak
 
Go, the one language to learn in 2014
Go, the one language to learn in 2014Go, the one language to learn in 2014
Go, the one language to learn in 2014Andrzej Grzesik
 
JDD2014: GO! The one language you have to try in 2014 - Andrzej Grzesik
JDD2014: GO! The one language you have to try in 2014 - Andrzej GrzesikJDD2014: GO! The one language you have to try in 2014 - Andrzej Grzesik
JDD2014: GO! The one language you have to try in 2014 - Andrzej GrzesikPROIDEA
 
Python language data types
Python language data typesPython language data types
Python language data typesHarry Potter
 
Python language data types
Python language data typesPython language data types
Python language data typesHoang Nguyen
 
Python language data types
Python language data typesPython language data types
Python language data typesYoung Alista
 
Python language data types
Python language data typesPython language data types
Python language data typesLuis Goldster
 
Python language data types
Python language data typesPython language data types
Python language data typesTony Nguyen
 
Python language data types
Python language data typesPython language data types
Python language data typesFraboni Ec
 
Python language data types
Python language data typesPython language data types
Python language data typesJames Wong
 
Introduction to programming - class 11
Introduction to programming - class 11Introduction to programming - class 11
Introduction to programming - class 11Paul Brebner
 
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...ETH Zurich
 
Ruslan.shevchenko: most functional-day-kiev 2014
Ruslan.shevchenko: most functional-day-kiev 2014Ruslan.shevchenko: most functional-day-kiev 2014
Ruslan.shevchenko: most functional-day-kiev 2014Ruslan Shevchenko
 
Learn You a Functional JavaScript for Great Good
Learn You a Functional JavaScript for Great GoodLearn You a Functional JavaScript for Great Good
Learn You a Functional JavaScript for Great GoodMike Harris
 
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
 

Similar a Go Containers (20)

JVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in KotlinJVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in Kotlin
 
Combinator parsing
Combinator parsingCombinator parsing
Combinator parsing
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?
 
Go, the one language to learn in 2014
Go, the one language to learn in 2014Go, the one language to learn in 2014
Go, the one language to learn in 2014
 
JDD2014: GO! The one language you have to try in 2014 - Andrzej Grzesik
JDD2014: GO! The one language you have to try in 2014 - Andrzej GrzesikJDD2014: GO! The one language you have to try in 2014 - Andrzej Grzesik
JDD2014: GO! The one language you have to try in 2014 - Andrzej Grzesik
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Introduction to programming - class 11
Introduction to programming - class 11Introduction to programming - class 11
Introduction to programming - class 11
 
go.ppt
go.pptgo.ppt
go.ppt
 
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
 
Ruslan.shevchenko: most functional-day-kiev 2014
Ruslan.shevchenko: most functional-day-kiev 2014Ruslan.shevchenko: most functional-day-kiev 2014
Ruslan.shevchenko: most functional-day-kiev 2014
 
Learn You a Functional JavaScript for Great Good
Learn You a Functional JavaScript for Great GoodLearn You a Functional JavaScript for Great Good
Learn You a Functional JavaScript for Great Good
 
Music as data
Music as dataMusic as data
Music as data
 
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
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 

Más de jgrahamc

Better living through microcontrollers
Better living through microcontrollersBetter living through microcontrollers
Better living through microcontrollersjgrahamc
 
Big O London Meetup April 2015
Big O London Meetup April 2015Big O London Meetup April 2015
Big O London Meetup April 2015jgrahamc
 
How to launch and defend against a DDoS
How to launch and defend against a DDoSHow to launch and defend against a DDoS
How to launch and defend against a DDoSjgrahamc
 
Lua: the world's most infuriating language
Lua: the world's most infuriating languageLua: the world's most infuriating language
Lua: the world's most infuriating languagejgrahamc
 
Software Debugging for High-altitude Balloons
Software Debugging for High-altitude BalloonsSoftware Debugging for High-altitude Balloons
Software Debugging for High-altitude Balloonsjgrahamc
 
Highlights of Go 1.1
Highlights of Go 1.1Highlights of Go 1.1
Highlights of Go 1.1jgrahamc
 
That'll never work!
That'll never work!That'll never work!
That'll never work!jgrahamc
 
HAB Software Woes
HAB Software WoesHAB Software Woes
HAB Software Woesjgrahamc
 
Javascript Security
Javascript SecurityJavascript Security
Javascript Securityjgrahamc
 

Más de jgrahamc (9)

Better living through microcontrollers
Better living through microcontrollersBetter living through microcontrollers
Better living through microcontrollers
 
Big O London Meetup April 2015
Big O London Meetup April 2015Big O London Meetup April 2015
Big O London Meetup April 2015
 
How to launch and defend against a DDoS
How to launch and defend against a DDoSHow to launch and defend against a DDoS
How to launch and defend against a DDoS
 
Lua: the world's most infuriating language
Lua: the world's most infuriating languageLua: the world's most infuriating language
Lua: the world's most infuriating language
 
Software Debugging for High-altitude Balloons
Software Debugging for High-altitude BalloonsSoftware Debugging for High-altitude Balloons
Software Debugging for High-altitude Balloons
 
Highlights of Go 1.1
Highlights of Go 1.1Highlights of Go 1.1
Highlights of Go 1.1
 
That'll never work!
That'll never work!That'll never work!
That'll never work!
 
HAB Software Woes
HAB Software WoesHAB Software Woes
HAB Software Woes
 
Javascript Security
Javascript SecurityJavascript Security
Javascript Security
 

Último

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 

Último (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 

Go Containers

  • 1. Go Containers January 23, 2014 John Graham-Cumming www.cloudflare.com!
  • 2. Six interesting containers •  From pkg/container! •  container/list! •  container/ring! •  container/heap
 ! •  Built in •  map! •  slice •  Channels as queues www.cloudflare.com!
  • 3. container/list! •  Doubly-linked list implementation •  Uses interface{} for values l := list.New()! e0 := l.PushBack(42)! e1 := l.PushFront(13)! Pity  there’s  no  ‘map’   e2 := l.PushBack(7)! func4on   l.InsertBefore(3, e0)! l.InsertAfter(196, e1)! l.InsertAfter(1729, e2)! ! for e := l.Front(); e != nil; e = e.Next() {! fmt.Printf("%d ", e.Value.(int))! }! e.Value  to  get  the   fmt.Printf("n")! stored  value   ! ! 13 196 3 42 7 1729! www.cloudflare.com!
  • 4. container/list! All  work  on   elements  not  values     l.MoveToFront(e2)! l.MoveToBack(e1)! Returns  the  value   l.Remove(e0)! removed   ! for e := l.Front(); e != nil; e = e.Next() {! fmt.Printf("%d ", e.Value.(int))! }! fmt.Printf("n")! ! ! 7 196 3 1729 13! www.cloudflare.com!
  • 5. container/ring! •  A circular ‘list’ parus := []string{”major", “holsti”, “carpi”}! ! r := ring.New(len(parus))! for i := 0; i < r.Len(); i++ {! r.Value = parus[i]! r = r.Next()! }! ! r.Do(func(x interface{}) {! fmt.Printf(“Parus %sn”, x.(string))! })! •  Move n elements through ring r.Move(n)! www.cloudflare.com!
  • 6. container/heap! •  Implements a “min-heap” (i.e. tree where each node is the “minimum” element in its subtree) •  Needs a notion of “Less” and a way to “Swap” www.cloudflare.com!
  • 7. container/heap! •  The single most confusing definition in all of Go type Interface interface {! sort.Interface! Push(x interface{}) // add x as element Len()! Pop() interface{} // remove/return element Len()-1! }! ! // Note that Push and Pop in this interface are for ! // package heap's implementation to call. To add and ! // remove things from the heap, use heap.Push and! // heap.Pop.! www.cloudflare.com!
  • 8. container/heap! •  Simple example type OrderedInts []int! ! func (h OrderedInts) Len() int { return len(h) }! func (h OrderedInts) Less(i, j int) bool {! return h[i] < h[j]! }! func (h OrderedInts) Swap(i,j int) {h[i],h[j]=h[j],h[i]}! func (h *OrderedInts) Push(x interface{}) {! !*h = append(*h, x.(int))! }! func (h *OrderedInts) Pop() interface{} {! !old := *h! !n := len(old)-1! !x := old[n]! !*h = old[:n]! !return x! }! www.cloudflare.com!
  • 9. container/heap! •  Using a heap h := &OrderedInts{33,76,55,24,48,63,86,83,83,12}! ! heap.Init(h)! ! fmt.Printf("min: %dn", (*h)[0])! ! for h.Len() > 0 {! fmt.Printf("%d ", heap.Pop(h))! }! ! fmt.Printf(“n”)! www.cloudflare.com!
  • 10. container/heap! •  Heaps are useful for... •  Make a priority queue •  Sorting •  Graph algorithms www.cloudflare.com!
  • 12. map! •  Maps are typed dictionary := make(map[string]string)! dictionary := map[string]string{}! ! •  They are not concurrency safe •  Use a lock or channel for concurrent read/write access counts := struct{! sync.RWMutex! m map[string]int! }{m: make(map[string]int)}! ! counts.RLock()! fmt.Printf(“foo count”, counts.m[“foo”]! counts.RUnlock()! ! counts.Lock()! counts.m[“foo”] += num_foos! counts.Unlock()! ! www.cloudflare.com! Mul4ple  readers,   one  writer  
  • 13. map iteration m := map[string]int{! "bar”: 54,! "foo”: 42,! "baz”: -1,! }! ! for k := range m {! // k is foo, bar, baz! }! ! for _, v := range m {! // v is 54, 42, -1 in some order! }! ! for k, v := range m {! // k and v are as above! }! www.cloudflare.com! Order  of  itera4on  is   undefined    
  • 14. Common map operations •  Remove an element delete(dictionary, “twerking”)! •  Test presence of an element definition, present := dictionary[“hoopy”]! ! _, present := dictionary[“sigil”]! •  Missing element gives a “zero” value fmt.Printf(“[%s]n”, dictionary[“ewyfgwyegfweygf”])! ! []! www.cloudflare.com!
  • 16. Slices •  A slice is part of an array var arrayOfInts [256]int! ! var part []int = arrayOfInts[2:6]! •  arrayOfInts is 256 ints contiguously in memory 0   1   2   3   4   5   6   7   8   9   10   11   12   ! ! •  part consists of a pointer (to arrayOfInts[2]) and a length (4) www.cloudflare.com!
  • 17. Slice passing •  A slice is passed (like everything else) by copy var arrayOfInts [256]int! ! var part []int = arrayOfInts[2:6]! ! func fill(s []int) {! for i, _ := range s {! s[i] = i*2! }! ! s = s[1:]! }! Does  nothing  to   ! part! fill(part)! fmt.Printf(“%#v”, part)! ! % ./slice! []int{0, 2, 4, 6}! www.cloudflare.com! Contents  of  s  can  be   modified   Changes  contents  of   underlying  array  
  • 18. Slice passing, part 2 •  Can pass a pointer to a slice to modify the slice var arrayOfInts [256]int! ! var part intSlice = arrayOfInts[2:6] ! ! Contents  of  s  can  be   type intSlice []int! modified  and  s  can   func (s *intSlice) fill() {! be  changed   for i, _ := range *s {! (*s)[i] = i*2! }! *s = (*s)[1:]! Changes  part! }! ! part.fill()! fmt.Printf("%#vn", part)! ! % ./slice! []int{2, 4, 6}! www.cloudflare.com!
  • 19. Slice iteration prime := []int{2, 3, 5, 7, 11}! ! for i := range prime {! // i is 0, 1, 2, 3, 4! }! ! for _, e := range prime{! // e is 2, 3, 5, 7, 11! }! ! for i, e := range prime {! // i and e as above! }! www.cloudflare.com!
  • 20. Copying slices •  copy builtin morePrimes := make([]int, len(primes), 2*cap(primes))! ! copy(morePrimes, primes)! •  copy allows source and destination to overlap primes := [10]int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29}! odds := primes[1:7]! ! odds = odds[0:len(odds)+1]! copy(odds[4:], odds[3:])! odds[3] = 9! fmt.Printf("%#vn", odds)! ! []int{3, 5, 7, 9, 11, 13, 17}! www.cloudflare.com!
  • 21. Appending slices s := []int{1, 3, 6, 10}! t := []int{36, 45, 55, 66, 78}! ! s = append(s, 15)! Adding  individual   elements   s = append(s, 21, 28)! ! s = append(s, t...)! ! Adding  an  en4re   nu := append([]int(nil), s...)! slice   ! s = append(s, s...)! Copying  a  slice  (use   ! copy  instead)   fmt.Printf(“%#vn”, s)! ! []int{1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78}! www.cloudflare.com!
  • 23. A buffered channel is a FIFO queue •  A typed queue of up to 10 Things queue := make(chan Thing, 10) ! •  Get the next element from the queue if there is one select {! case t := <-queue: // got one! default: // queue is empty! } ! •  Add to queue if there’s room select {! case queue <- t: // added to queue! default: // queue is full! }! www.cloudflare.com!
  • 25. Perhaps heretical •  But... I wish Go had some generics •  interface{} is like void *; Type assertions similar to casts l := list.New()! l.PushFront("Hello, World!")! v := l.Front()! i := v.Value.(int)! % go build l.go! % ./l! panic: interface conversion: interface is string, not int! ! goroutine 1 [running]:! runtime.panic(0x49bdc0, 0xc210041000)! !/extra/go/src/pkg/runtime/panic.c:266 +0xb6! main.main()! !/extra/src/mc/generic.go:12 +0xaa! www.cloudflare.com!
  • 26. Sources etc. •  Slides and code samples for this talk: https://github.com/cloudflare/jgc-talks/tree/master/ Go_London_User_Group/Go_Containers •  All my talks (with data/code) on the CloudFlare Github https://github.com/cloudflare/jgc-talks •  All my talks on the CloudFlare SlideShare http://www.slideshare.net/cloudflare www.cloudflare.com!