1 回答

TA貢獻1877條經(jīng)驗 獲得超6個贊
這是一個遞歸示例https://go.dev/play/p/cLm-QHydM37
這導(dǎo)致以下迭代
當長度為 5 時:map[1:5 2:25 3:125 4:625 5:3125]
當長度為 3 時:map[1:3 2:9 3:27 4:0 5:0]
package main
import "fmt"
func main() {
count := map[int]int{1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
// This recursion is based on https://gobyexample.com/recursion
var loopFunc func(current int, data []string)
loopFunc = func(current int, data []string) {
for i := 0; (i < len(data)) && (len(data) != current-1); i++ {
count[current] = count[current] + 1
loopFunc(current+1, data)
}
}
loopFunc(1, make([]string, 5))
fmt.Println(count)
}
我可能沒有完全正確的循環(huán)邏輯,但這應(yīng)該是您繼續(xù)前進的跳板。
- 1 回答
- 0 關(guān)注
- 91 瀏覽
添加回答
舉報