-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (55 loc) · 1.59 KB
/
main.go
File metadata and controls
65 lines (55 loc) · 1.59 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2020 ActiveState Software. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"time"
)
var exit1 = flag.Bool("exit1", false, "exit the script with exit code 1")
var sleep = flag.Bool("sleep", false, "sleep for an hour, basically never return unless interrupted")
var fillBuffer = flag.Bool("fill-buffer", false, "print a string with 100,00 characters")
var stutter = flag.Bool("stutter", false, "print 50 messages with 50 ms delays")
func main() {
c := make(chan os.Signal, 1)
defer close(c)
signal.Notify(c, os.Interrupt)
flag.Parse()
fmt.Println("an expected string")
if *sleep {
/* This will listen to a ctrl-c event for up to two hours
* Notice: That it is *only* necessary to watch for an interrupt
* signal on Windows. On Linux&MacOS the interrupt signal would
* always break the control flow, whereas on Windows it is not really
* clear when and how this is happening.
*/
select {
case <-time.After(1 * time.Hour):
fmt.Println("returning after an hour, this will never happen")
case sig := <-c:
fmt.Printf("received %v\n", sig)
os.Exit(123)
}
}
if *fillBuffer {
for i := 0; i < 300; i++ {
os.Stdout.WriteString(fmt.Sprintf(":%03d:", i))
for j := 5; j < 80; j++ {
os.Stdout.WriteString(fmt.Sprintf("%d", j%10))
}
}
os.Stdout.Write([]byte("\n"))
}
if *stutter {
for i := 0; i < 20; i++ {
fmt.Printf("stuttered %d times\n", i+1)
time.Sleep(50 * time.Millisecond)
}
}
if *exit1 {
os.Exit(1)
}
}