问题描述
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)的形式发送它,则无法使用其方法,只会看到:
我想将此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
}