FAQ
Hi Carlo,

The point of the exercise is to implement a type that satisfies the
image.Image interface.

For that the type you defined (MyImage) should declare the three methods
declared in image.Image. Your solution compiles and works ... but it's a
bit of cheating :-)

You are just doing a proxy of the image.Image type returned by

The solution for this (and all the other) exercise is available on

Cheers,

On Wed, Mar 19, 2014 at 12:43 PM, Carlo Tambuatco wrote:

Okay...I've got one working "solution" here, it's rather trivial, though...

package main

import (
"image"
"image/color"
)

type MyImage struct{
s image.Image
}

func (m MyImage) ColorModel() color.Model {
return m.s.ColorModel()
}

func (m MyImage) Bounds() image.Rectangle {
return m.s.Bounds()
}

func (m MyImage) At(x, y int) color.Color {
a := uint8(x)
b := uint8(y)
return color.RGBA{a, b, 255, 255}
}

func main() {
m := MyImage{image.NewRGBA(image.Rect(0, 0, 200, 200))}
pic.ShowImage(m)
}

It's rather trivial, creating an image variable within the struct and
calling Image's
already defined methods on it. So if you have a method returning something
like,
color.Color or image.Rectangle, you can return anything that implements
their
respective methods? That was what I was unclear about in the exercise, I
think,
whether I had to create objects that implemented Color and Rectangle's
methods...

You follow?

On Wednesday, March 19, 2014 2:59:20 PM UTC-4, Steve McCoy wrote:

Rectangle is a struct, so you can just use it. You can also use existing
implementations from the color package for Model and Color. E.g. RGBAModel
and RGBA.
On Wednesday, March 19, 2014 2:48:15 PM UTC-4, Carlo Tambuatco wrote:

I just want a solution to the following exercise on the tour.golang.orgwebsite,
exercise #62.

http://tour.golang.org/#62

The exercise is to implement the image interface:

type Image interface {
// ColorModel returns the Image's color model.
ColorModel() color <http://golang.org/pkg/image/color/>.Model <http://golang.org/pkg/image/color/#Model>
// Bounds returns the domain for which At can return non-zero color.
// The bounds do not necessarily contain the point (0, 0).
Bounds() Rectangle <http://golang.org/pkg/image/#Rectangle>
// At returns the color of the pixel at (x, y).
// At(Bounds().Min.X, Bounds().Min.Y) returns the upper-left pixel of the grid.
// At(Bounds().Max.X-1, Bounds().Max.Y-1) returns the lower-right one.
At(x, y int <http://golang.org/pkg/builtin/#int>) color <http://golang.org/pkg/image/color/>.Color <http://golang.org/pkg/image/color/#Color>
}

I'm just learning Go, and I've worked through every exercise except this
one, because
of the different interfaces involved such as Color, Rectangle, etc, I'm
having trouble
with whether or not I should implement every interface involved because
Color,
Rectangle, and color.Model etc are interfaces themselves...

provide one
possible solution so I can study it?

--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an

--
--
Francesc Campoy

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

Related Discussions

 view thread | post posts ‹ prev | 5 of 5 | next ›
Discussion Overview
 group golang-nuts categories go posted Mar 19, '14 at 6:48p active Mar 19, '14 at 8:31p posts 5 users 4 website golang.org

4 users in discussion

Content

People

Support

Translate

site design / logo © 2021 Grokbase