Skip to content

Conversation

Plus-Jia
Copy link

修复中Map方法的数组越界错误

问题描述

在原文“3)实现对应作用的功效 (Map-functionality)”示例代码中,Map方法中存在数组越界错误,与示例“cars.go”中代码不一致

错误代码

func (cs Cars) Map(f func(car *Car) Any) []Any {
    result := make([]Any, 0)  // 创建长度为0的空切片
    ix := 0
    cs.Process(func(c *Car) {
        result[ix] = f(c)     // 错误:尝试访问result[0],但切片长度为0
        ix++
    })
    return result
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant