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)