1

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.

Boo Yan Jiong
  • 2,491
  • 5
  • 17
  • 31

1 Answers1

3

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
maerics
  • 151,642
  • 46
  • 269
  • 291
  • so, i declare the function as `createCanvas(mw *imagick.MagickWand)`, and inside the function, I use the `mw` as if `err = *mw.NewImage(200,100,pw)`? Trying now... :) – Boo Yan Jiong Sep 28 '18 at 15:43
  • receive error message: `createCanvas passes lock by value: .../imagick.v3/imagick.MagickWand contains sync.Once contains sync.Mutex`... I try to change the `*` position around the code, but can't get it done. – Boo Yan Jiong Sep 28 '18 at 16:04
  • Thanks for helping. What you said is correct... After careful examination, I notice I naively debug the wrong line. Thanks. – Boo Yan Jiong Sep 30 '18 at 09:40