1 回答
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊
類型斷言(https://tour.golang.org/methods/15)是當(dāng)你有一個(gè)并想要訪問具體值時(shí)所需要的:interface{}
type TestArgs struct {
name string
customAssert func(interface{})
}
// Define the test case with TestArgs.
tests := []TestArgs{
{
name: "Test A",
customAssert: func(param interface{}) {
// Some code
array, ok := param.([]*pb.TypeA)
if !ok {
t.Fatal("assertion error TypeA")
}
// use array
},
},
{
name: "Test B",
customAssert: func(param interface{}) {
// Some code
array, ok := param.([]*pb.TypeB)
if !ok {
t.Fatal("assertion error TypeB")
}
// use array
},
},
}
- 1 回答
- 0 關(guān)注
- 159 瀏覽
添加回答
舉報(bào)
