码迷,mamicode.com
首页 > 其他好文 > 详细

A Tour of Go Exercise: Slices

时间:2014-10-27 06:54:12      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   sp   div   on   log   

Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values.

The choice of image is up to you. Interesting functions include x^y,(x+y)/2, and x*y.

(You need to use a loop to allocate each []uint8 inside the [][]uint8.)

(Use uint8(intValue) to convert between types.

 

package main

import "code.google.com/p/go-tour/pic"

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

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

 

A Tour of Go Exercise: Slices

标签:style   blog   color   io   for   sp   div   on   log   

原文地址:http://www.cnblogs.com/ghgyj/p/4053352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!