gpt4 book ai didi

go - 在 GO 中测试同名函数

转载 作者:IT王子 更新时间:2023-10-29 01:22:31 25 4
gpt4 key购买 nike

在 go 中可以编写特定于结构的函数。

type one struct{}

func (o *one) fly() {}

我的问题是,如果有两个同名但指向不同结构的函数,您如何测试一个函数。

type one struct{}

func (o *one) fly() {}

type two struct{}

func (t *two) fly() {}

因为 GO 测试的格式是 TestXxx (t *testing.T) {}我不确定如何单独测试每个功能。谢谢

最佳答案

TestXxx 只是一种命名约定。 Xxx 可以是任何你想要的,但是 Test(带有 BenchmarkExample)是必需的。因此,声明 2 个测试函数 — TestOneFlyTestTwoFly,仅此而已。或者您可以在 TestFly 中测试两者,在一次测试中初始化两个结构。

关于go - 在 GO 中测试同名函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22032295/

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