简体   繁体   English

如何将magickWand引用传递给函数

[英]How to pass magickWand reference to functions

May I know how to pass imagick.MagickWand struct to functions and apply it methods on it? 我可以知道如何将imagick.MagickWand结构传递给函数并将其应用于它上面吗? It seems that the imagick.NewMagickWand return the type of *imagick.MagickWand , isn't it? 似乎imagick.NewMagickWand返回*imagick.MagickWand的类型,不是吗?

I can't get it done, keep getting the error message: ERROR_WAND: ContainsNoImages `MagickWand-0' . 我无法完成它,不断收到错误消息: 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? 如何将正确的struct引用传递给函数,以便mw可以在这些函数中连续使用

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. 谢谢。

If mw has type *imagick.MagickWand then *mw has type imagick.MagickWand . 如果mw有类型*imagick.MagickWand那么*mw类型为imagick.MagickWand

That is to say, mw is a pointer to that type and the * operator dereferences the pointer to the type itself . 也就是说, mw指向该类型的指针*运算符取消引用指向该类型的指针。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM