-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path交叉打印.go
More file actions
42 lines (42 loc) · 696 Bytes
/
交叉打印.go
File metadata and controls
42 lines (42 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"fmt"
"sync"
)
func main(){
a:=0
var lock1,lock2 sync.Mutex
var wait sync.WaitGroup
ch:=make(chan string)
English:="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
number:="0123456789"
wait.Add(1)
lock2.Lock()
go func(){
for k,value:=range English{
lock1.Lock()
ch<-string(value)
if (k%2==1&&a==0) {lock2.Unlock()} else {lock1.Unlock()}
}
wait.Done()
}()
go func(){
for k,value:=range number{
lock2.Lock()
ch<-string(value)
if k==9 {
a=1
lock1.Unlock()
return
}
if k%2==1 {lock1.Unlock()} else {lock2.Unlock()}
}
}()
go func(){
wait.Wait()
close(ch)
}()
for char := range ch {
fmt.Print(char)
}
}