gpt4 book ai didi

go - 如果它具有相同的 'signature' 为什么不能使用来自不同包的类型? golang

转载 作者:IT王子 更新时间:2023-10-29 00:35:10 26 4
gpt4 key购买 nike

我想知道为什么这些函数不适用于同类类型?请参阅以下伪函数。

Playground :http://play.golang.org/p/ZG1jU8H2ZJ

package main

type typex struct {
email string
id string
}

type typey struct {
email string
id string
}

func useType(t *typex) {
// do something
}

func main() {
x := &typex{
id: "somethng",
}
useType(x) // works

y := &typey{
id: "something",
}
useType(y) // doesn't work ??
}

最佳答案

至于为什么 - Y 不是 X 那么为什么它会起作用?

如果它们确实相同,您可以通过将 typey 转换为 typex 轻松克服此问题:

useType((*typex)(y))

但是,如果它们是相同的,为什么会有两种类型呢?

关于go - 如果它具有相同的 'signature' 为什么不能使用来自不同包的类型? golang ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24383812/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com