goimagemagickmagickwand

How to pass magickWand reference to functions


May I know how to pass imagick.MagickWand struct to functions and apply it methods on it? It seems that the imagick.NewMagickWand return the type of *imagick.MagickWand, isn't it?

I can't get it done, keep getting the error message: ERROR_WAND: ContainsNoImages `MagickWand-0'.

How to pass proper struct reference to the function so that the mw can be continuously used in those functions?

func generateImage() error {
    // skip error handling
    var err error 

    mw := imagick.NewMagickWand()
    defer mw.Destroy()

    err = createCanvas(mw, "red") // create canvas
    err = compositeIcon(mw, "c:/icon.png") // add icon
    err = addText(mw, "Hello world") // add text

    err = mw.WriteImage("c:/output") // get the output
}

func createCanvas(mw *imagick.MagickWand, color string) error {
    // skip error handling
    var err error
    pw := imagick.NewPixelWand()
    defer pw.Destroy()

    pw.SetColor("blue")
    err = mw.NewImage(200, 100, pw)
    return nil
}

May please help? Newbie here. :)


Update:

The example that I gave is correct. Passing by reference is done correctly in the example above, I received the error because I overlook my code and debug the wrong lines. Sorry for confusion.

Thanks.


Solution

  • If mw has type *imagick.MagickWand then *mw has type imagick.MagickWand.

    That is to say, mw is a pointer to that type and the * operator dereferences the pointer to the type itself.

    mw := imagick.NewMagickWand() // Now mw is a *imagick.MagickWand
    *mw // Is a imagick.MagickWand