image.Image is a Go interface for two-dimensional images. Its At(x, y) method returns the color.Color at a pixel coordinate, making it suitable for reading raster images regardless of their concrete representation. section 9.2.2 section 9.7
Use image.Image as the decoded-image boundary, then choose a concrete type when pixel operations require more convenience or control:
- Decode PNG data with image/png.Decode, which returns image.Image. section 9.2.2
- Call At(x, y) for general pixel access, then convert the returned color.Color to RGBA when needed. section 9.2.2
- Type-assert to image.RGBA when RGBAAt and Set are more convenient for reading or modifying pixels. section 9.2.2 section 9.7
Opening a maze image
func openMaze(imagePath string) (*image.RGBA, error) { img, err := png.Decode(file); if err != nil { return nil, err }; rgba, ok := img.(*image.RGBA); if !ok { return nil, fmt.Errorf("expected RGBA image, got %T", img) }; return rgba, nil } A paletted PNG can decode successfully yet fail the RGBA assertion, so the concrete image type must be checked explicitly.
