首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将目录不同部分中定义的方法调用到任何您想要的位置

将目录不同部分中定义的方法调用到任何您想要的位置
EN

Stack Overflow用户
提问于 2021-08-07 20:32:01
回答 1查看 60关注 0票数 0

我很难调用在与我的主文件不同的包中定义和实现的函数。这里是我正在研究的代码库结构的复制品。

repo/collegeutil/handlecourse.go

,我有

代码语言:javascript
运行
复制
package collegeutil

func (clg *CollegeUtil) AddCourse(ctc context.Context, param1 string, param2 string) {
    #Function implementation 
}

repo/collegeutil/student.go.中定义的结构CollegeUtil在这份文件里,我

代码语言:javascript
运行
复制
package collegeutil

type CollegeUtil struct {
   subSvc college.subjectService
}

在另一个文件中,也就是在repo/college/student.go,中

代码语言:javascript
运行
复制
package college

type subjectService interface {
   AddCourse(ctx context.Context, param1 string, param2 string)
}

现在,在我的repo/classes/main.go,中,我有以下内容

代码语言:javascript
运行
复制
package main

reply, err := college.subjectService.AddCourse(ctx, param1 string, param2 string)

但这不管用。我收到一个错误,上面写着college.subjectService.AddCourse.的参数太少,不能调用但论证的数量是正确的。显然,它在开始时需要多一个参数,这是参数中的college.subjectService值。这到底是什么??

我也试过collegeutil.CollegeUtil.AddCourse().但是这里我得到了“不能调用collegeutil.CollegeUtil上的指针方法AddCourse”

对上述代码库中的主代码调用AddCourse方法的正确方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-08 12:26:24

我可以在您的代码示例中看到一些未导出的项。试着先导出它们然后构建..。

导出subSvc

代码语言:javascript
运行
复制
package collegeutil

type CollegeUtil struct {
   SubSvc college.subjectService
}

Eport subjectService

代码语言:javascript
运行
复制
package college

type SubjectService interface {
   AddCourse(ctx context.Context, param1 string, param2 string)
}

和:

代码语言:javascript
运行
复制
reply, err := college.SubjectService.AddCourse(ctx, param1 string, param2 string)

这不是一个解决方案;因为我没有很高的声誉来评论,在这里补充道。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68695901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档