3

I'm just trying different things to learn go and understand the structures of how it works. Currently playing around with slices and custom types.

I have the following code which works fine and as expected.

package imgslice

import (
    "fmt"
    "image"
)

type imageData struct {
    position      int         // Image Number
    image         *image.RGBA // Image Store
    height        int         // Height In Pixels
    width         int         // Width In Pixels
}

func init() {
    fmt.Println("Starting")

    lbl := &[]imageData{}
    println(lbl)

    InitImage(lbl, 3)

    fmt.Printf("%#v\n", lbl)


}

// Initalise the imageData arrray
func InitImage(l *[]imageData, images int) {
    var i int
    var newRecord imageData

    for i = 0; i < images; i++ {
        newRecord = imageData{position: i}

        *l = append(*l, newRecord)

    }

    return
}

I'm trying to re-write the InitImage function to work as a method (i think that is the correct term). But I get the error:

invalid receiver type *[]imageData ([]imageData is not a defined type)

(edit: Error is on the func (l *[]imageData) InitImageNew(images int) { line)

The only reason I want to do this is a) learning to see if it can be done and b) stylistically I think I prefer this do having the slice as an extra argument.

func (l *[]imageData) InitImageNew(images int) {
    var i int
    var newRecord imageData

    for i = 0; i < images; i++ {
        newRecord = imageData{position: i}

        *l = append(*l, newRecord)

    }

    return
}

Looking at this answer: Function declaration syntax: things in parenthesis before function name

It seems like it should be possible - however this answer seems to say it's not possible: Golang monkey patching.

Is it possible to rewrite this so I could do

lbl := &[]imageData{}
lbl.InitImageNew(4)

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
BlueIris
  • 173
  • 1
  • 8
  • 5
    A receiver has to be a defined type. `[]Type` is not a defined type. You can define it: `type ArrImageData []imageData` and use `*ArrImageData` as receiver. But then you have to convert []imageData to ArrImageData to call the method. – Burak Serdar May 22 '20 at 16:36
  • Many Thanks for the answer I've solved it now thanks for the help. – BlueIris May 22 '20 at 17:35

2 Answers2

7

You can only defined methods on named types (or pointers to named types). []Type is a compound type. You could make it a named type by defining it:

type TypeSlice []Type

And then define methods on it.

This is covered in the spec section on types.

Adrian
  • 42,911
  • 6
  • 107
  • 99
  • Fair, pointers are treated differently than any other compound type which could trip up some users. Updated answer. – Adrian May 22 '20 at 16:59
-1

This question was the top one for my search, so i'll add what my issue was: I had type MyType = struct { (with the equal sign) as the root cause. Removing equal sign helped.

Denis V
  • 78
  • 5
  • Yes, if you get this error even for non-compound types, check your type declaration. If it contains an equals try removing it. For example change `type MyType = struct { name string }` to `type MyType struct { name string }`. – Joseph King Jul 03 '23 at 16:10