<span style="color:#009900;">//递归输出组合内容</span>func combineloop(arr []string, r []string, i int, n int, output chan<- []string) {if n <= 0 {return}
rlen := len(r) - nalen := len(arr)for j := i; j < alen; j++ {r[rlen] = arr[j]if n == 1 {or := make([]string, len(r))copy(or, r)output <- or} else {combineloop(arr, r, j+1, n-1, output)}
}
}//对数组进行组合
func GetCombineMatch(arr []string, n int) []string {var matchArr []stringoutput := make(chan []string)r := make([]string, n)go func() {combineloop(arr, r, 0, n, output)close(output)}()
for arr := range output {var str stringfor i, each := range arr {if i == 0 {str = each} else {str = fmt.Sprintf("%v,%v", str, each)}
}
matchArr = append(matchArr, str)fmt.Println(arr)
}
return matchArr}
有疑问加站长微信联系(非本文作者)