11. Hacker 101
this presentation contains code
that code is probably broken
if this bothers you - fix it
it’s called a learning experience
12. Hacker 101
this presentation contains code
that code is probably broken
if this bothers you - fix it
it’s called a learning experience
and will make you a better persontm
22. package
organisation import
var, const, type
declaration
func, interface, map, struct, chan
if ... else
switch ... case ... fallthrough ... default
select ... case
control flow for
for ... range
break, continue
go, goto, defer, return
26. superclass modules
class modules
message
instance
27. superclass modules
class modules
type asserted
instance
28. instance inherited
class class
expressed
type
modules
29. instance inherited
class class
expressed
type
modules
30. instance inherited
class class
expressed
type
modules
31. instance inherited
class class
expressed
type
modules
32. class Counter
attr_writer :count
def initialize
@count = 0
end
def Tick
@count += 1
end
end
class DoubleCounter < Counter
def Tick
super
@count += 1
end
end
n = new(DoubleCounter)
puts n.Tick
puts n.superclass.Tick
33. class Counter
attr_writer :count
def initialize
@count = 0
end
def Tick
@count += 1
end
end
class DoubleCounter < Counter
def Tick
super
@count += 1
end
end
n = new(DoubleCounter)
puts n.Tick
puts n.superclass.Tick
34. class Counter
attr_writer :count
def initialize
@count = 0
end
def Tick
@count += 1
end
end
class DoubleCounter < Counter
def Tick
super
@count += 1
end
end
n = new(DoubleCounter)
puts n.Tick
puts n.superclass.Tick
35. class Counter
attr_writer :count
def initialize
@count = 0
end
def Tick
@count += 1
end
end
class DoubleCounter < Counter
def Tick
super
@count += 1
end
end
n = new(DoubleCounter)
puts n.Tick
puts n.superclass.Tick
36. class Counter
attr_writer :count
def initialize
@count = 0
end
def Tick
@count += 1
end
end
class DoubleCounter < Counter
def Tick
super
@count += 1
end
end
n = new(DoubleCounter)
puts n.Tick
puts n.superclass.Tick
37. class Counter
attr_writer :count
def initialize
@count = 0
end
def Tick
@count += 1
end
end
class DoubleCounter < Counter
def Tick
super
@count += 1
end
end
n = new(DoubleCounter)
puts n.Tick
puts n.superclass.Tick
39. down a rabbit hole
instances are their own classes
and all classes are mutable at runtime
40. down a rabbit hole
instances are their own classes
all classes are mutable at runtime
so inheritance pathways can be altered
41. down a rabbit hole
instances are their own classes
and all classes are mutable at runtime
so inheritance pathways can be altered
making Ruby very flexible
42. down a rabbit hole
instances are their own classes
and all classes are mutable at runtime
so inheritance pathways can be altered
making Ruby very flexible
at the cost of type uncertainty
43. down a rabbit hole
instances are their own classes
and all classes are mutable at runtime
so inheritance pathways can be altered
making Ruby very flexible
at the cost of type uncertainty
which really bugs computer scientists
44. type in Go is safetm
clearly defined areas of doubt and uncertainty
45. memory method
layout set
static
type
embedded
types
46. memory method
layout set
static
type
embedded
types
47. memory method
layout set
static
type
embedded
types
48. memory method
layout set
static
type
embedded
types
83. down a rabbit hole
an object has known static type
this fixed type is determined at linking
84. down a rabbit hole
an object has known static type
this fixed type is determined at linking
no new types can be created at runtime
85. down a rabbit hole
an object has known static type
this fixed type is determined at linking
no new types can be created at runtime
so dynamism is bounded to a fixed set
86. down a rabbit hole
an object has known static type
this fixed type is determined at linking
no new types can be created at runtime
so dynamism is bounded to this fixed set
and computer scientists are happier
126. func throwsPanic(f func()) (b bool) {
defer func() {
if x := recover(); x != nil {
b = true
}
}()
f()
return
}
127. func throwsPanic(f func()) (b bool) {
defer func() {
if x := recover(); x != nil {
b = true
}
}()
f()
return
}
128. func throwsPanic(f func()) (b bool) {
defer func() {
if x := recover(); x != nil {
b = true
}
}()
f()
return
}
129. func throwsPanic(f func()) (b bool) {
defer func() {
if x := recover(); x != nil {
b = true
}
}()
f()
return
}
130. func throwsPanic(f func()) (b bool) {
defer func() {
if x := recover(); x != nil {
b = true
}
}()
f()
return
}
131. func throwsPanic(f func()) (b bool) {
defer func() {
if x := recover(); x != nil {
b = true
}
}()
f()
return
}
132. func throwsPanic(f func()) (b bool) {
defer func() {
if x := recover(); x != nil {
b = true
}
}()
f()
return
}
133. func throwsPanic(f func()) (b bool) {
defer func() {
if x := recover(); x != nil {
b = true
}
}()
f()
return
}
134. import "fmt"
import "path"
import "runtime"
func StackTrace() {
var stack_trace []uintptr
var my_path string
runtime.Callers(1, stack_trace)
for i, u := range stack_trace {
if f := runtime.FuncForPC(u); f != nil {
file, line := f.FileLine(u)
switch filepath, filename := path.Split(file); {
case i == 0: my_path = filepath
case my_path != filepath: fmt.Printf("%v:%v", filename, line)
}
} else {
fmt.Println("(unknown)")
}
}
}
135. import "fmt"
import "path"
import "runtime"
func StackTrace() {
var stack_trace []uintptr
var my_path string
runtime.Callers(1, stack_trace)
for i, u := range stack_trace {
if f := runtime.FuncForPC(u); f != nil {
file, line := f.FileLine(u)
switch filepath, filename := path.Split(file); {
case i == 0: my_path = filepath
case my_path != filepath: fmt.Printf("%v:%v", filename, line)
}
} else {
fmt.Println("(unknown)")
}
}
}
136. import "fmt"
import "path"
import "runtime"
func StackTrace() {
var stack_trace []uintptr
var my_path string
runtime.Callers(1, stack_trace)
for i, u := range stack_trace {
if f := runtime.FuncForPC(u); f != nil {
file, line := f.FileLine(u)
switch filepath, filename := path.Split(file); {
case i == 0: my_path = filepath
case my_path != filepath: fmt.Printf("%v:%v", filename, line)
}
} else {
fmt.Println("(unknown)")
}
}
}
137. import "fmt"
import "path"
import "runtime"
func StackTrace() {
var stack_trace []uintptr
var my_path string
runtime.Callers(1, stack_trace)
for i, u := range stack_trace {
if f := runtime.FuncForPC(u); f != nil {
file, line := f.FileLine(u)
switch filepath, filename := path.Split(file); {
case i == 0: my_path = filepath
case my_path != filepath: fmt.Printf("%v:%v", filename, line)
}
} else {
fmt.Println("(unknown)")
}
}
}
138. import "fmt"
import "path"
import "runtime"
func StackTrace() {
var stack_trace []uintptr
var my_path string
runtime.Callers(1, stack_trace)
for i, u := range stack_trace {
if f := runtime.FuncForPC(u); f != nil {
file, line := f.FileLine(u)
switch filepath, filename := path.Split(file); {
case i == 0: my_path = filepath
case my_path != filepath: fmt.Printf("%v:%v", filename, line)
}
} else {
fmt.Println("(unknown)")
}
}
}
139. import "fmt"
import "path"
import "runtime"
func StackTrace() {
var stack_trace []uintptr
var my_path string
runtime.Callers(1, stack_trace)
for i, u := range stack_trace {
if f := runtime.FuncForPC(u); f != nil {
file, line := f.FileLine(u)
switch filepath, filename := path.Split(file); {
case i == 0: my_path = filepath
case my_path != filepath: fmt.Printf("%v:%v", filename, line)
}
} else {
fmt.Println("(unknown)")
}
}
}
140. import "fmt"
import "path"
import "runtime"
func StackTrace() {
var stack_trace []uintptr
var my_path string
runtime.Callers(1, stack_trace)
for i, u := range stack_trace {
if f := runtime.FuncForPC(u); f != nil {
file, line := f.FileLine(u)
switch filepath, filename := path.Split(file); {
case i == 0: my_path = filepath
case my_path != filepath: fmt.Printf("%v:%v", filename, line)
}
} else {
fmt.Println("(unknown)")
}
}
}
141. import "fmt"
import "path"
import "runtime"
func StackTrace() {
var stack_trace []uintptr
var my_path string
runtime.Callers(1, stack_trace)
for i, u := range stack_trace {
if f := runtime.FuncForPC(u); f != nil {
file, line := f.FileLine(u)
switch filepath, filename := path.Split(file); {
case i == 0: my_path = filepath
case my_path != filepath: fmt.Printf("%v:%v", filename, line)
}
} else {
fmt.Println("(unknown)")
}
}
}
142. import "fmt"
import "path"
import "runtime"
func StackTrace() {
var stack_trace []uintptr
var my_path string
runtime.Callers(1, stack_trace)
for i, u := range stack_trace {
if f := runtime.FuncForPC(u); f != nil {
file, line := f.FileLine(u)
switch filepath, filename := path.Split(file); {
case i == 0: my_path = filepath
case my_path != filepath: fmt.Printf("%v:%v", filename, line)
}
} else {
fmt.Println("(unknown)")
}
}
}
143. import "fmt"
import "path"
import "runtime"
func StackTrace() {
var stack_trace []uintptr
var my_path string
runtime.Callers(1, stack_trace)
for i, u := range stack_trace {
if f := runtime.FuncForPC(u); f != nil {
file, line := f.FileLine(u)
switch filepath, filename := path.Split(file); {
case i == 0: my_path = filepath
case my_path != filepath: fmt.Printf("%v:%v", filename, line)
}
} else {
fmt.Println("(unknown)")
}
}
}
144. import "fmt"
import "path"
import "runtime"
func StackTrace() {
var stack_trace []uintptr
var my_path string
runtime.Callers(1, stack_trace)
for i, u := range stack_trace {
if f := runtime.FuncForPC(u); f != nil {
file, line := f.FileLine(u)
switch filepath, filename := path.Split(file); {
case i == 0: my_path = filepath
case my_path != filepath: fmt.Printf("%v:%v", filename, line)
}
} else {
fmt.Println("(unknown)")
}
}
}
222. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
223. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
224. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
225. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
226. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
227. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
228. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
229. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
230. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
231. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
232. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
233. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
234. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
235. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
236. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
237. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
238. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
239. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
240. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
241. func ByteSlice(i interface{}) []byte {
switch i := i.(type) {
case []byte: return i
case MemoryBlock: return i.ByteSlice()
}
var header *SliceHeader
switch v := ValueOf(i); value.Kind() {
case Interface, Ptr: header = valueHeader(v.Elem())
case Slice: h, s, _ := SliceHeader(i)
header = Scale(h, s, 1)
case String: s := v.Get()
h := *(*StringHeader)(unsafe.Pointer(&s))
header = &SliceHeader{ h.Data, h.Len, h.Len }
default: header = valueHeader(v)
}
return unsafe.Unreflect(_BYTE_SLICE, unsafe.Pointer(header)).([]byte)
}
242. go f(){}() / yourself
/
map/reduce for all the family
243. package main
import "fmt"
func main() {
var c chan int
c = make(chan int)
limit := 16
go func() {
for i := limit; i > 0; i-- {
fmt.Print(<-c) produces:
} 0110011101011010
}()
for i := limit; i > 0; i-- {
select {
case c <- 0:
case c <- 1:
}
}
}
244. package main
import "fmt"
func main() {
var c chan int
c = make(chan int)
limit := 16
go func() {
for i := limit; i > 0; i-- {
fmt.Print(<-c) produces:
} 0110011101011010
}()
for i := limit; i > 0; i-- {
select {
case c <- 0:
case c <- 1:
}
}
}
245. package main
import "fmt"
func main() {
var c chan int
c = make(chan int)
limit := 16
go func() {
for i := limit; i > 0; i-- {
fmt.Print(<-c) produces:
} 0110011101011010
}()
for i := limit; i > 0; i-- {
select {
case c <- 0:
case c <- 1:
}
}
}
246. package main
import "fmt"
func main() {
var c chan int
c = make(chan int)
limit := 16
go func() {
for i := limit; i > 0; i-- {
fmt.Print(<-c) produces:
} 0110011101011010
}()
for i := limit; i > 0; i-- {
select {
case c <- 0:
case c <- 1:
}
}
}