[英]Golang cannot use type as type in argument
I am new to Go language and I am trying to get familiar with interfaces and their assignability. 我是Go语言的新手,我试图熟悉接口及其可分配性。
I am trying to pass an argument from a struct
into a function
which is imported from another package
. 我试图将参数从struct
传递到从另一个package
导入的function
中。
main.go package: main.go软件包:
package main
import {
anotherPackage
}
type I1 interface {
anotherPackage.I2
}
type T1 struct {
*anotherPackage.S1
}
type T2 struct {
variable1 T1
}
type T3 struct {
variable2 T2
}
func handler() {
var fromI I
var input = T3{}
template := fromI.ExportedFunction(input.T3.variable1)
}
func main() {
handler()
}
anotherPackage.go package anotherPackage.go软件包
package anotherPackage
type I2 interface {
ExportedFunction(S1)
}
type S1 struct {
Path string
File string
}
type S2 struct {}
func (s2 *S2) ExportedFunction(s1 S1) {}
I keep getting an error: 我不断收到错误消息:
cannot use input.T3.variable1 (type T1) as type anotherPackage.S1 in argument to fromI.ExportedFunction
You cannot do that in Go like other Object-oriented language because Go does not support polymorphism. 您不能像其他面向对象的语言那样在Go中执行此操作,因为Go不支持多态。 How about using T1 interface as the parameter to ExportedFunction instead of S1. 如何使用T1接口作为ExportedFunction而不是S1的参数。 example https://play.golang.org/p/72hgbSwNkaS 范例https://play.golang.org/p/72hgbSwNkaS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.