0%

Tiktok print in Golang

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
package main

import (
"fmt"
"time"
)

func main() {
// Create a channel for synchronization
tikTokChan := make(chan bool)

// Start the goroutine for "tik"
go func() {
for {
fmt.Println("tik")
time.Sleep(1 * time.Second) // Wait for 1 second
tikTokChan <- true // Signal the "tok" goroutine
<-tikTokChan // Wait for the "tok" goroutine
}
}()

time.Sleep(1 * time.Second)
// Start the goroutine for "tok"
go func() {
for {
<-tikTokChan // Wait for the "tik" goroutine
fmt.Println("tok")
time.Sleep(1 * time.Second) // Wait for 1 second
tikTokChan <- true // Signal the "tik" goroutine
}
}()

// Use a channel to wait indefinitely or until the program is terminated
forever := make(chan bool)
<-forever
}