英文:
Trying to pass a type ("object") created in program 1 to a function in program 2
问题
我已经花了相当多的时间学习Python,现在正在尝试学习Go语言。我正在尝试在Go中做一些我经常在Python中做的事情:在一个程序中创建一个对象,然后将该对象传递给另一个方法(或函数)。
我知道Go语言没有对象,但我也认为我不应该重新定义已经定义过的类型。以下是我的示例代码:
程序1:
import (
"github.com/bndr/gojenkins"
"bitbucket.org/elsammons/senjink/actions"
)
func main() {
jenkins := gojenkins.CreateJenkins(url)
_, err := jenkins.Init()
if err != nil {
panic(err)
}
jobs := actions.GetAllJobs(jenkins)
}
程序2:
package actions
import (
"github.com/bndr/gojenkins"
)
func GetAllJobs(jenkins gojenkins.Jenkins) gojenkins.Job {
return jenkins.GetAllJobNames()
}
我尝试过其他方法,比如将类型定义为注释掉的形式,使用(jenkins *Jenkins)
等等... 但是我仍然遇到错误,显然我漏掉了一些东西。
非常感谢您在这个过程中给予的任何指导。
英文:
I have spent quite some time with python and am now trying to learn golang. I'm trying to do something in golang that I do often with python; essentially create an object in one program but then pass that object to a method (or function) in another.
I know golang doesn't have objects, but I'm also of the opinion that I should not have to define a type that has already been defined. My sample code follows:
program 1:
import (
"github.com/bndr/gojenkins"
"bitbucket.org/elsammons/senjink/actions"
)
func main () {
jenkins := gojenkins.CreateJenkins(url)
_, err := jenkins.Init()
if err != nil {
panic(err)
}
jobs = actions.GetAllJobs(jenkins)
}
Program 2:
package actions
import (
"github.com/bndr/gojenkins"
)
//type Jenkins gojenkins.Jenkins
//type Jobs gojenkins.Job
func GetAllJobs(jenkins gojenkins.Jenkins) jobNames gojenkins.Job {
return jenkins.GetAllJobNames()
}
I have tried other approaches as well, just as defining the type as commented out and using (jenkins *Jenkins) etc... However I continue to get errors so I'm obviously missing something.
Appreciate any instruction here that might help me on my journey.
答案1
得分: 1
对于您提供的错误,
bitbucket.org/elsammons/senjink/actions ./search.go:10: 语法错误: 意外的gojenkins在顶层声明之后
您应该修复函数返回类型的语法。如果jobNames
是您的返回变量的名称(顺便说一句,这不是推荐的做法..),您应该用括号括起来同时包围变量名和类型:
func GetAllJobs(jenkins gojenkins.Jenkins) (jobNames gojenkins.Job) {
您的代码中似乎还有其他编译错误(例如,GetAllJobs
的返回值与gojenkins.Jenkins.GetAllJobNames
的返回值不匹配),但这应该可以解决您目前的问题。
关于您的问题,关于如何将一个对象从一个程序/库传递到另一个程序/库,Golang的做法与其他语言类似:只需使用另一方期望的确切类型传递对象,您应该没问题。
英文:
For the error you provided,
bitbucket.org/elsammons/senjink/actions ./search.go:10: syntax error: unexpected gojenkins after top level declaration
You should fix the syntax of your function's return type. If jobNames
is the name of your return variable (which, by the way, isn't a recommended practice..), you should surround both variable name and type with parentheses:
func GetAllJobs(jenkins gojenkins.Jenkins) (jobNames gojenkins.Job) {
There seems to be other compile errors in your code (e.g. the return of GetAllJobs
doesn't match the return of gojenkins.Jenkins.GetAllJobNames
), but this should fix what you're asking for now.
Regarding your question, about passing an object from one program/library to another one, Golang does it just like every language: just pass the object with the exact type that the other side is expecting and you should be fine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论