【发布时间】:2017-10-10 18:44:20
【问题描述】:
考虑这段代码:
func main() {
items := func1()
for _, v := range items {
v.Status = append(v.Status, false)
}
fmt.Println(items)
}
//TestCaseItem represents the test case
type TestCaseItem struct {
Actual []string
Expected []string
Status []bool
}
func func1() []TestCaseItem {
var tc = make([]TestCaseItem, 0)
var item = &TestCaseItem{}
item.Actual = append(item.Actual, "test1")
item.Actual = append(item.Actual, "test2")
tc = append(tc, *item)
return tc
}
我有一个 TestCaseItem 结构类型的切片。在那个结构中,我有一部分字符串和布尔属性。首先我调用func1 函数来获取一些数据,然后遍历该切片并尝试在其中追加更多数据,但是这段代码的输出是[{[test1 test2] [] []}] 布尔值在哪里?
我觉得问题出在[]TestCaseItem 因为它是一个保存值而不是指针的切片,也许我会错过某事。有人可以解释一下吗?
【问题讨论】: