Skip to content

Instantly share code, notes, and snippets.

@aesteve aesteve/slices.go
Created Jan 20, 2016

Embed
What would you like to do?
Slices (with closure)
package main
import "golang.org/x/tour/pic"
func mult(x, y int) uint8 {
return uint8( x * y )
}
func mid(x, y int) uint8 {
return uint8( (x + y) / 2 )
}
func pow(x, y int) uint8 {
return uint8( x^y )
}
func generateWithFunc(generator func(x, y int) uint8) func(dx int, dy int) [][]uint8 {
return func(dx, dy int) [][]uint8 {
tab := make([][]uint8, dx)
for x := range tab {
tab[x] = make([]uint8, dy)
for y := range tab[x] {
tab[x][y] = generator(x, y)
}
}
return tab
}
}
func main() {
pic.Show(generateWithFunc(mid))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.