微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

golang中的类型不一致,不能将<Type>用作* Type

如何解决golang中的类型不一致,不能将<Type>用作* Type

你好,我在Golang中有下一个代码

func createPDF(count int)   {
    pdfFile := pdf.NewMaroto(consts.Portrait,consts.A4)
    pdfFile.SetPageMargins(10,15,10)
    writeGroups(pdfFile)
}

func  writeGroups(pdf *pdf.Maroto) {
    //Do something
}

如果我以(&pdfFile)的形式发送它,则无法使用其方法,只会看到:

enter image description here

我想将此pdf变量作为指向writeGroups方法的指针传递,我在Golang中得到了不能使用类型(Maroto)作为* pdf.Maroto类型。我是该语言的新手,是否有可能。问题是什么意思?

谢谢

解决方法

假设您正在使用https://github.com/johnfercher/maroto public class HomeController : Controller { private List<UserProfile> users = new List<UserProfile>() { new UserProfile(){Id="1",Province="1",Country="1",City="1"},new UserProfile(){Id="2",Province="2",Country="2",City="3"},}; public IActionResult Index() { return View(users); } [HttpGet] public async Task<IActionResult> Edit(string id) { var user = users.Where(a => a.Id == id).FirstOrDefault(); ViewBag.Province = new SelectList(new LocationController().provinces,"Id","Name",user.Province); ViewBag.City = new SelectList(new LocationController().cities,user.City); ViewBag.Country = new SelectList(new LocationController().countries,user.Country); EditUserViewModel modelVM = new EditUserViewModel { Country = user.Country,Province = user.Province,City = user.City,}; return View(modelVM); } [HttpPost] public IActionResult Edit(string city,string province,string country) { return RedirectToAction("Index"); } } 是一个接口,您可以通过引用将其传递到函数中,而无需将其变成指向接口的指针。

因此,如果您将功能更改为

pdf.Maroto

您现在应该可以在pdf.Maroto上调用接口函数。这些功能定义是为pdf.Maroto而不是* pdf.Maroto定义的,这就是为什么您不能使用它们的原因。

或者,如果您确定需要指向pdf.Maroto函数的指针,则可以取消引用该指针,然后调用所需的函数。

func  writeGroups(pdf pdf.Maroto) {
    //Do something
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。