Skip to content

Instantly share code, notes, and snippets.

@tetsuok
Created April 2, 2012 02:40
Show Gist options
  • Star 60 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save tetsuok/2280162 to your computer and use it in GitHub Desktop.
Save tetsuok/2280162 to your computer and use it in GitHub Desktop.
An answer of the exercise: Slices on a tour of Go
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)
}
@bryceleue
Copy link

package main

import (
	"golang.org/x/tour/pic"
)

func Pic(dx, dy int) [][]uint8 {
	s := make([][]uint8, dy)
	for i := range s {
		s[i] = make([]uint8, dx)

		for j := range s[i] {
			s[i][j] = uint8((i + j) / 2 >> 8 + (i + j) / 2 % (i + 2) % (j + 2))
		}
	}
	return s
}

func main() {
	pic.Show(Pic)
}

golangthangs

@mikurei
Copy link

mikurei commented Oct 12, 2023

Very suspicious pattern

package main

import "golang.org/x/tour/pic"

const (
	VAL_MAX uint8 = 255
	AMOG_DX uint8 = 10
	AMOG_DY uint8 = 10
)
// ඞ
var AMOGUS = [AMOG_DY][AMOG_DX]uint8{
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 2, 2, 2, 0, 0, 0},
	{0, 0, 0, 2, 2, 1, 1, 1, 0, 0},
	{0, 0, 2, 2, 2, 2, 2, 2, 0, 0},
	{0, 0, 2, 2, 2, 2, 2, 2, 0, 0},
	{0, 0, 0, 2, 2, 2, 2, 2, 0, 0},
	{0, 0, 0, 2, 0, 0, 0, 2, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}

func Shader(x, y int) uint8 {
	x_u := uint8(x) % AMOG_DX
	y_u := uint8(y) % AMOG_DY
	return AMOGUS[y_u][x_u]*(VAL_MAX>>1) + 1

}

func Pic(dx, dy int) [][]uint8 {
	buf := make([][]uint8, dy)
	for y := range buf {
		column := make([]uint8, dx)
		for x := range column {
			column[x] = Shader(x, y)
		}
		buf[y] = column
	}
	return buf
}

func main() {
	pic.Show(Pic)
}

sus

@hello2333
Copy link

package main

import "golang.org/x/tour/pic"
// import "fmt"

func pointValue1(x, y int) uint8 {
	return uint8((x + y) / 2)
}

func pointValue2(x, y int) uint8 {
	return uint8(x * y)
}

func pointValue3(x, y int) uint8 {
	return uint8(x ^ y)
}

func Pic(dx, dy int) [][]uint8 {
	// initialize slice rows with dy length
	var matrix [][]uint8
	
	// initialize the i'th element of rows with dx length slice
	for i := 0; i < dy; i ++ {
		// var array_rows [dx]uint8
		var array_rows []uint8
		// update the value of each point
		for j := 0; j < dx; j ++ {
			// array_rows[j] = pointValue1(i, j)
			array_rows = append(array_rows, pointValue3(i, j))
		}
		// matrix = append(matrix, array_rows[0:])
		matrix = append(matrix, array_rows)
	}
	
	// fmt.Println(matrix)
	return matrix
}

func main() {
	pic.Show(Pic)
}

go测试

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment