Hello. I am trying to understand how to work with channels on the example
the problem of obtaining permutations.
I took as a basis the code here and almost managed to change it.
However, the program does not work as I expect.
Here is my code:
package main
import "fmt"
func Generate(alphabet string, length int) chan string {
c := make(chan string)
go func(c chan string) {
defer close(c)
CreatePermute(c, alphabet, length)
}(c)
return c
}
func CreatePermute(c chan string, alphabet string, length int) {
if length == 0 {
return
}
for _, v := range alphabet {
c <- string(v)
for Y := range Generate(alphabet, length-1) {
c <- string(v) + Y
}
}
}
func main() {
for perm := range Generate("abc", 2) {
fmt.Println(perm)
}
}
My questions:
- Why does the program stops working, if comment out 22 line (c <- string(v))
- How fix this?
I would appreciate for any help.