package main
import("fmt""sync")funcmain(){var wg sync.WaitGroup
letterCh :=make(chanstruct{})
numberCh :=make(chanstruct{})
wg.Add(1)goprintLetters(letterCh, numberCh,&wg)
wg.Add(1)goprintNumbers(letterCh, numberCh,&wg)
letterCh <-struct{}{}// 启动字母打印
wg.Wait()}funcprintLetters(letterCh, numberCh chanstruct{}, wg *sync.WaitGroup){defer wg.Done()
letters :="ABCDEFGHIJKLMNOPQRSTUVWXYZ"for i :=0; i <len(letters); i++{<-letterCh
fmt.Printf("%c ", letters[i])
numberCh <-struct{}{}}}funcprintNumbers(letterCh, numberCh chanstruct{}, wg *sync.WaitGroup){defer wg.Done()
numbers :="123456789"for i :=0; i <len(numbers); i++{<-numberCh
fmt.Printf("%c ", numbers[i])
letterCh <-struct{}{}}}
5、 字符串对位交换
6、 返回一个数组所有可能排列
package main
import("fmt")funcpermute(nums []int)[][]int{
result :=[][]int{}generatePermutations(nums,0,&result)return result
}funcgeneratePermutations(nums []int, index int, result *[][]int){if index ==len(nums)-1{// 将当前排列加入结果集
temp :=make([]int,len(nums))copy(temp, nums)*result =append(*result, temp)return}for i := index; i <len(nums); i++{// 交换元素位置
nums[index], nums[i]= nums[i], nums[index]// 递归生成下一个位置的排列generatePermutations(nums, index+1, result)// 恢复原始数组,以便下一次交换
nums[index], nums[i]= nums[i], nums[index]}}funcmain(){
nums :=[]int{1,2,3}
result :=permute(nums)// 打印所有排列for_, perm :=range result {
fmt.Println(perm)}}