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
image.NewRGBA. Instead the exercises asks you to define your own image.

The solution for this (and all the other) exercise is available on
https://code.google.com/p/go-tour/source/browse/solutions

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 (
"code.google.com/p/go-tour/pic"
"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...

I'm obviously not thinking about this the right way. Could someone just
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
email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
--
Francesc Campoy
http://twitter.com/francesc

--
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.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 5 of 5 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 19, '14 at 6:48p
activeMar 19, '14 at 8:31p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase