Skip to content

Instantly share code, notes, and snippets.

@ifreesec

ifreesec/练习切片.md

Last active Jul 17, 2018
Embed
What would you like to do?
go 语言之旅,练习

练习:切片
实现 Pic。它应当返回一个长度为 dy 的切片,其中每个元素是一个长度为 dx,元素类型为 uint8 的切片。当你运行此程序时,它会将每个整数解释为灰度值(好吧,其实是蓝度值)并显示它所对应的图像。图像的选择 x*log(y)

package main

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

func Pic(dx, dy int) [][]uint8 {
	s := make([][]uint8, dx)
	for i := 0; i < dx; i++ {
		temp :=make([]uint8, dy)
		for j := 0; j < dy; j++ {
			temp[j] = uint8(float64(i) * math.Log(float64(j)))
		}
		s[i] = temp
	} 
	return s
}

func main() {
	pic.Show(Pic)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment