gpt4 book ai didi

去泛型 - 联合

转载 作者:行者123 更新时间:2023-12-04 13:05:23 30 4
gpt4 key购买 nike

我正在通过修改我为处理 slice 而创建的库来玩转泛型。我有一个 Difference接受 slice 并返回仅在其中一个 slice 中找到的唯一元素列表的函数。
我修改了函数以使用泛型,并且我正在尝试编写具有不同类型(例如字符串和整数)的单元测试,但是在使用 union type 时遇到了问题. Here是我现在拥有的:

type testDifferenceInput[T comparable] [][]T
type testDifferenceOutput[T comparable] []T
type testDifference[T comparable] struct {
input testDifferenceInput[T]
output testDifferenceOutput[T]
}

func TestDifference(t *testing.T) {
for i, tt := range []testDifference[int] {
testDifference[int]{
input: testDifferenceInput[int]{
[]int{1, 2, 3, 3, 4},
[]int{1, 2, 5},
[]int{1, 3, 6},
},
output: []int{4, 5, 6},
},
} {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
actual := Difference(tt.input...)

if !isEqual(actual, tt.output) {
t.Errorf("expected: %v %T, received: %v %T", tt.output, tt.output, actual, actual)
}
})
}
}
我希望能够在同一个表测试中测试 int 或 string。这是我尝试过的:
type intOrString interface {
int | string
}
type testDifferenceInput[T comparable] [][]T
type testDifferenceOutput[T comparable] []T
type testDifference[T comparable] struct {
input testDifferenceInput[T]
output testDifferenceOutput[T]
}

func TestDifference(t *testing.T) {
for i, tt := range []testDifference[intOrString] {
testDifference[int]{
input: testDifferenceInput[int]{
[]int{1, 2, 3, 3, 4},
[]int{1, 2, 5},
[]int{1, 3, 6},
},
output: []int{4, 5, 6},
},
testDifference[string]{
input: testDifferenceInput[string]{
[]string{"1", "2", "3", "3", "4"},
[]string{"1", "2", "5"},
[]string{"1", "3", "6"},
},
output: []string{"4", "5", "6"},
},
} {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
actual := Difference(tt.input...)

if !isEqual(actual, tt.output) {
t.Errorf("expected: %v %T, received: %v %T", tt.output, tt.output, actual, actual)
}
})
}
}
但是,在运行它时,我收到以下错误:
$ go version
go version dev.go2go-55626ee50b linux/amd64

$ go tool go2go test
arrayOperations_unit_test.go2:142:6: expected ';', found '|' (and 5 more errors)
为什么它提示我的 intOrString界面?
编辑 #1 - 我可以确认, @Nulo's帮助,那个 gotip确实有效,我现在明白为什么我不能使用 intOrString作为一种类型 - 它应该是一个约束。
但是,在我的表测试中找到一些混合整数和字符串的方法仍然会很好......
$ gotip version
go version devel go1.18-c812b97 Fri Oct 29 22:29:31 2021 +0000 linux/amd64

$ gotip test
# github.com/adam-hanna/arrayOperations/go2 [github.com/adam-hanna/arrayOperations/go2.test]
./arrayOperations_unit_test.go:152:39: interface contains type constraints
./arrayOperations_unit_test.go:152:39: intOrString does not satisfy intOrString
./arrayOperations_unit_test.go:155:6: incompatible type: cannot use []int{…} (value of type []int) as []intOrString value
./arrayOperations_unit_test.go:156:6: incompatible type: cannot use []int{…} (value of type []int) as []intOrString value
./arrayOperations_unit_test.go:157:6: incompatible type: cannot use []int{…} (value of type []int) as []intOrString value
./arrayOperations_unit_test.go:159:13: incompatible type: cannot use []int{…} (value of type []int) as testDifferenceOutput[intOrString] value
./arrayOperations_unit_test.go:163:6: incompatible type: cannot use []string{…} (value of type []string) as []intOrString value
./arrayOperations_unit_test.go:164:6: incompatible type: cannot use []string{…} (value of type []string) as []intOrString value
./arrayOperations_unit_test.go:165:6: incompatible type: cannot use []string{…} (value of type []string) as []intOrString value
./arrayOperations_unit_test.go:167:13: incompatible type: cannot use []string{…} (value of type []string) as testDifferenceOutput[intOrString] value
./arrayOperations_unit_test.go:152:39: too many errors
FAIL github.com/adam-hanna/arrayOperations/go2 [build failed]

最佳答案

我已通过 gotip 传递了您的代码它使用了更先进的提案实现,并且不会提示那部分代码,所以我认为问题出在 go2go 的初始实现上。
请注意,您的实现将不起作用,因为您绝对可以在类型断言表达式中使用参数接口(interface),但是您不能像在 testDifference[intOrString] 中所做的那样使用带有类型列表的接口(interface)。

关于去泛型 - 联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69772512/

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