https://github.com/danielgatis/go-ctrlc
Gracefully quit when you press ctrl-c.
https://github.com/danielgatis/go-ctrlc
ctrl-c go golang
Last synced: 2 months ago
JSON representation
Gracefully quit when you press ctrl-c.
- Host: GitHub
- URL: https://github.com/danielgatis/go-ctrlc
- Owner: danielgatis
- License: mit
- Created: 2022-01-06T02:02:57.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-06T19:08:06.000Z (over 3 years ago)
- Last Synced: 2025-02-08T20:47:33.118Z (4 months ago)
- Topics: ctrl-c, go, golang
- Language: Go
- Homepage:
- Size: 3.91 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go - Ctrlc
[](https://goreportcard.com/report/github.com/danielgatis/go-ctrlc)
[](https://raw.githubusercontent.com/danielgatis/go-ctrlc/master/LICENSE)
[](https://godoc.org/github.com/danielgatis/go-ctrlc)Gracefully quit when you press ctrl-c.
## Install
```bash
go get -u github.com/danielgatis/go-ctrlc
```And then import the package in your code:
```go
import "github.com/danielgatis/go-ctrlc"
```### Example
An example described below is one of the use cases.
```go
package mainimport (
"fmt"
"os"
"time""github.com/danielgatis/go-ctrlc"
)func main() {
tick := time.NewTicker(time.Second)
exit := make(chan struct{})ctrlc.Watch(func() {
exit <- struct{}{}
})for {
select {
case <-exit:
fmt.Println("bye!")
os.Exit(0)
case <-tick.C:
fmt.Println(time.Now())
}
}
}
``````
❯ go run main.go
2022-01-06 12:08:46.015813 -0300 -03 m=+1.000276951
2022-01-06 12:08:47.016224 -0300 -03 m=+2.000688652
^Cbye!
```### License
Copyright (c) 2021-present [Daniel Gatis](https://github.com/danielgatis)
Licensed under [MIT License](./LICENSE)
### Buy me a coffee
Liked some of my work? Buy me a coffee (or more likely a beer)