Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

At a Loss with Images Exercise in Tour of Go

 
Ranch Hand
Posts: 224
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I'm on page "127.0.0.1:3999/methods/25" with title "Exercise: Images" that says:

with example code (labeled "exercise-images.go"):

The prior page defined the Image interface:

How in the world do I implement an interface for a type with methods that return a {color.Model} or a {color.Color}? I don't even know what those types are. I'm at a loss with this exercise. Can anyone give me any pointers here?
 
Sheriff
Posts: 14741
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The image and color prefixes refer to standard Go packages: https://golang.org/pkg

I think they just put those prefixes in that text to provide some context. You can also have the following import to avoid having to use the prefixes explicitly in your code.

The documentation for image.Image is here: https://golang.org/pkg/image/#Image

You'll need to familiarize yourself with the package documentation to get the most out of the tour. Fortunately, the documentation is quite comprehensive and what's more, many of the code examples are executable right from the documentation pages.
 
Junilu Lacar
Sheriff
Posts: 14741
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also https://godoc.org/golang.org/x/tour/pic

When you're instructed to write your own Image type and implement the necessary methods, that means you have to write something like this:


Unlike Java, Go does not require to explicitly declare that your type implements an interface. As long as all the necessary methods are present, the type is assumed to be an implementation.
 
Kevin Simonson
Ranch Hand
Posts: 224
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Unlike Java, Go does not require to explicitly declare that your type implements an interface. As long as all the necessary methods are present, the type is assumed to be an implementation.


Okay, I tried:

which looked to me like it should work, but that generated error message:

So it looks like the Go compiler can't locate package "color"; my import of simply "color" didn't do any good. Can anybody tell me what I should import so that I can see "color"?
 
Junilu Lacar
Sheriff
Posts: 14741
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, it is important to become familiar with the documentation of Go's standard packages. Go packages are hierarchical and the color package is actually under image so your import would be "image/color" not just "color".

See the import example on the package documentation page https://golang.org/pkg/image/color/

When you do that, your reference would be color.RGBA as indicated in the Tour example instead of image.color.RGBA
 
Kevin Simonson
Ranch Hand
Posts: 224
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Again, it is important to become familiar with the documentation of Go's standard packages. Go packages are hierarchical and the color package is actually under image so your import would be "image/color" not just "color".

See the import example on the package documentation page https://golang.org/pkg/image/color/

When you do that, your reference would be color.RGBA as indicated in the Tour example instead of image.color.RGBA


Now my code is:

and the error message is:

Is there a documentation page that lists the details for the Image interface?
 
Kevin Simonson
Ranch Hand
Posts: 224
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found and read the documentation at "https://golang.org/pkg/image/#Image". I still don't see what's wrong with my function:

Is there some reason I can't have a pointer receiver for my function? Should I just write it as:

I'm a little confused here.
 
Kevin Simonson
Ranch Hand
Posts: 224
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:See also https://godoc.org/golang.org/x/tour/pic

When you're instructed to write your own Image type and implement the necessary methods, that means you have to write something like this:


Unlike Java, Go does not require to explicitly declare that your type implements an interface. As long as all the necessary methods are present, the type is assumed to be an implementation.


Never mind. I got some pretty cool results with code:

Looks like I've completed this exercise.
 
It's never done THAT before. Explain it to me tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!