A Tour of Go Exercise: Slices

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)
}
时间: 2024-10-26 15:22:00

A Tour of Go Exercise: Slices的相关文章

Exercise: Slices (画图)

A Tour of Go Exercise: Slices https://tour.golang.org/moretypes/18 这道题目,提供了一个画图函数 (pic.Show), 可以生成图片. 这个函数,即 pic.Show(f func(int, int) [][]uint8), 可见,它接受一个函数做参数,题目要求的正是编写这个参数.答案如下: 这里面,依赖一个 package, 即 "golang.org/x/tour/pic" 我上 https://github.co

A Tour of Go Nil slices

The zero value of a slice is nil. A nil slice has a length and capacity of 0. (To learn more about slices, read the Slices: usage and internalsarticle.) package main import "fmt" func main() { var z []int fmt.Println(z, len(z), cap(z)) if z == n

A Tour of Go Making slices

Slices are created with the make function. It works by allocating a zeroed array and returning a slice that refers to that array: a := make([]int, 5) // len(a)=5 To specify a capacity, pass a third argument to make: b := make([]int, 0, 5) // len(b)=0

A Tour of Go Slicing slices

---恢复内容开始--- Slices can be re-sliced, creating a new slice value that points to the same array. The expression s[lo:hi] evaluates to a slice of the elements from lo through hi-1, inclusive. Thus s[lo:lo] is empty and s[lo:lo+1] has one element. packa

A Tour of Go Exercise: Images

Remember the picture generator you wrote earlier? Let's write another one, but this time it will return an implementation of image.Image instead of a slice of data. Define your own Image type, implement the necessary methods, and callpic.ShowImage. B

A Tour of Go Exercise: HTTP Handlers

Implement the following types and define ServeHTTP methods on them. Register them to handle specific paths in your web server. type String string type Struct struct { Greeting string Punct string Who string } For example, you should be able to regist

A Tour of Go Exercise: Loops and Functions

As a simple way to play with functions and loops, implement the square root function using Newton's method. In this case, Newton's method is to approximate Sqrt(x) by picking a starting point z and then repeating: To begin with, just repeat that calc

A Tour of Go Exercise: Fibonacci closure

Let's have some fun with functions. Implement a fibonacci function that returns a function (a closure) that returns successive fibonacci numbers. package main import "fmt" // fibonacci is a function that returns // a function that returns an int

A Tour of Go Exercise: Errors

Copy your Sqrt function from the earlier exercises and modify it to return an error value. Sqrt should return a non-nil error value when given a negative number, as it doesn't support complex numbers. Create a new type type ErrNegativeSqrt float64 an