我很难调用在与我的主文件不同的包中定义和实现的函数。这里是我正在研究的代码库结构的复制品。
在repo/collegeutil/handlecourse.go中
,我有
package collegeutil
func (clg *CollegeUtil) AddCourse(ctc context.Context, param1 string, param2 string) {
#Function implementation
}repo/collegeutil/student.go.中定义的结构CollegeUtil在这份文件里,我
package collegeutil
type CollegeUtil struct {
subSvc college.subjectService
}在另一个文件中,也就是在repo/college/student.go,中
package college
type subjectService interface {
AddCourse(ctx context.Context, param1 string, param2 string)
}现在,在我的repo/classes/main.go,中,我有以下内容
package main
reply, err := college.subjectService.AddCourse(ctx, param1 string, param2 string)但这不管用。我收到一个错误,上面写着college.subjectService.AddCourse.的参数太少,不能调用但论证的数量是正确的。显然,它在开始时需要多一个参数,这是参数中的college.subjectService值。这到底是什么??
我也试过collegeutil.CollegeUtil.AddCourse().但是这里我得到了“不能调用collegeutil.CollegeUtil上的指针方法AddCourse”
对上述代码库中的主代码调用AddCourse方法的正确方法是什么?
发布于 2021-08-08 12:26:24
我可以在您的代码示例中看到一些未导出的项。试着先导出它们然后构建..。
导出subSvc
package collegeutil
type CollegeUtil struct {
SubSvc college.subjectService
}Eport subjectService
package college
type SubjectService interface {
AddCourse(ctx context.Context, param1 string, param2 string)
}和:
reply, err := college.SubjectService.AddCourse(ctx, param1 string, param2 string)这不是一个解决方案;因为我没有很高的声誉来评论,在这里补充道。
https://stackoverflow.com/questions/68695901
复制相似问题