Created
April 2, 2012 02:40
-
-
Save tetsuok/2280162 to your computer and use it in GitHub Desktop.
An answer of the exercise: Slices on a tour of Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "code.google.com/p/go-tour/pic" | |
func Pic(dx, dy int) [][]uint8 { | |
// Allocate two-dimensioanl array. | |
a := make([][]uint8, dy) | |
for i := 0; i < dy; i++ { | |
a[i] = make([]uint8, dx) | |
} | |
// Do something. | |
for i := 0; i < dy; i++ { | |
for j := 0; j < dx; j++ { | |
switch { | |
case j % 15 == 0: | |
a[i][j] = 240 | |
case j % 3 == 0: | |
a[i][j] = 120 | |
case j % 5 == 0: | |
a[i][j] = 150 | |
default: | |
a[i][j] = 100 | |
} | |
} | |
} | |
return a | |
} | |
func main() { | |
pic.Show(Pic) | |
} |
hello2333
commented
Feb 10, 2024
package main
import (
"golang.org/x/tour/pic"
)
func Pic(dx, dy int) [][]uint8 {
result := make([][]uint8, dy)
for y := range result {
result[y] = make([]uint8, dx)
for x := range result[y] {
result[y][x] = uint8((x + y) / 2)
}
}
return result
}
func main() {
pic.Show(Pic)
}
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
slice:= make([][]uint8, dy, dy)
for y, _ := range slice {
slice[y] = make([]uint8, dx)
for x:= range slice {
i := uint8(y)
j := uint8(x)
slice[i][j] = (j * j * 100) + (i * i * 100)
}
}
return slice
}
func main() {
pic.Show(Pic)
}
package main
import (
"golang.org/x/tour/pic"
"math"
)
func Pic(dx, dy int) [][]uint8 {
bitmap := make([][]uint8, dy)
for i := 0; i < dy; i++ {
bitmap[i] = make([]uint8, dx)
}
for i := 0; i < dy; i++ {
for j := 0; j < dx; j++ {
bitmap[i][j] = uint8(math.Sqrt(float64(i)) * .01 * float64(i * j)) >> 2
}
}
return bitmap
}
func main() {
pic.Show(Pic)
}
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
img := make([][]uint8, dy)
for y := range dy {
row := make([]uint8, dx)
for x := range dx {
row[x] = uint8(x * y)
}
img[y] = row
}
return img
}
func main() {
pic.Show(Pic)
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment