英文:
Calling methods defined in different parts of the directory to wherever you want
问题
我正在努力解决一个问题,即如何调用在不同包中定义和实现的函数到我的主文件中。这里是我正在处理的代码库的结构副本。
在 repo/collegeutil/handlecourse.go 中,我有以下代码:
package collegeutil
func (clg *CollegeUtil) AddCourse(ctc context.Context, param1 string, param2 string) {
// 函数实现
}
CollegeUtil
是在 repo/collegeutil/student.go 中定义的结构体。在这个文件中,我有以下代码:
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)
但是这并不起作用。我得到了一个错误,错误信息是 too few arguments to call to college.subjectService.AddCourse。但是参数的数量是正确的。显然,它需要一个额外的参数,即作为参数的 college.subjectService
值的开头部分。这实际上是什么?
我还尝试了 collegeutil.CollegeUtil.AddCourse()
,但是这里我得到了一个错误 “can not call pointer method AddCourse on collegeutil.CollegeUtil”。
在上述代码库中,调用 AddCourse
方法到主函数的正确方式是什么?
英文:
I am struggling with calling a function defined and implemented in a different package to my main file. This here is a replica of the structure of the codebase I am working on.
in repo/collegeutil/handlecourse.go
, I have
package collegeutil
func (clg *CollegeUtil) AddCourse(ctc context.Context, param1 string, param2 string) {
#Function implementation
}
CollegeUtil is a struct defined in repo/collegeutil/student.go. In this file, I have
package collegeutil
type CollegeUtil struct {
subSvc college.subjectService
}
And in a different file, that is in repo/college/student.go, I have
package college
type subjectService interface {
AddCourse(ctx context.Context, param1 string, param2 string)
}
Now in my repo/classes/main.go, I have the following
package main
reply, err := college.subjectService.AddCourse(ctx, param1 string, param2 string)
But this is not working. I’m getting an error that says too few arguments to call to college.subjectService.AddCourse. But the number of arguments is correct. Apparently it requires one more parameter in the beginning which is of college.subjectService value in argument. What is this actually? ?
I also tried collegeutil.CollegeUtil.AddCourse(). But here I get “can not call pointer method AddCourse on collegeutil.CollegeUtil”
What is the correct way of calling AddCourse method to the main in the above codebase?
答案1
得分: 0
我可以看到你的代码示例中有一些未导出的项。请先导出它们并构建。
导出 subSvc
:
package collegeutil
type CollegeUtil struct {
SubSvc college.SubjectService
}
导出 subjectService
:
package college
type SubjectService interface {
AddCourse(ctx context.Context, param1 string, param2 string)
}
还有这部分代码:
reply, err := college.SubjectService.AddCourse(ctx, param1 string, param2 string)
这不是一个解决方案;由于我没有足够的声望来评论,所以在这里添加了这段代码。
英文:
I could see a few unexported items in your code sample. Try exporting them first and build..
Export subSvc
:
package collegeutil
type CollegeUtil struct {
SubSvc college.subjectService
}
Eport subjectService
:
package college
type SubjectService interface {
AddCourse(ctx context.Context, param1 string, param2 string)
}
And:
reply, err := college.SubjectService.AddCourse(ctx, param1 string, param2 string)
This is not a solution; since I don't have rough reputation to comment, adding here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论