{"id":605,"date":"2022-05-06T09:48:00","date_gmt":"2022-05-06T09:48:00","guid":{"rendered":"https:\/\/andrejacobs.org\/?p=605"},"modified":"2022-05-27T15:56:55","modified_gmt":"2022-05-27T15:56:55","slug":"learning-go-day-11","status":"publish","type":"post","link":"https:\/\/andrejacobs.org\/study-notes\/learning-go-day-11\/","title":{"rendered":"Learning Go – Day 11"},"content":{"rendered":"\n
Featured image by Egon Elbre<\/a><\/p>\n\n\n\n You can use the The following example will run in an infinite loop until you either press Adding GOPATH to $PATH<\/h3>\n
\n
$(go env GOPATH)\/bin<\/code> to my $PATH environment variable. For me this involved modifying my custom zsh resources.<\/li>\n<\/ul>\n
Handling OS signals<\/h3>\n
os\/signal<\/code> package to listen for signal notifications sent to your process.<\/p>\n
\n
make(chan os.Signal, 1)<\/code><\/li>\n
signal.Notify()<\/code><\/li>\n
signal.Stop()<\/code><\/li>\n<\/ul>\n
Ctrl+C<\/code> or kill the process.<\/p>\n
package main\n\nimport (\n\t"fmt"\n\t"os"\n\t"os\/signal"\n\t"syscall"\n)\n\nfunc main() {\n\tfmt.Printf("Running forever until you either press Ctrl+C or `kill %d`\\n", syscall.Getpid())\n\n\tsigCh := make(chan os.Signal, 1)\n\n\tsignal.Notify(sigCh, syscall.SIGINT, syscall.SIGABRT)\n\n\tfor {\n\t\tselect {\n\t\tcase recvSig := <-sigCh:\n\t\t\tsignal.Stop(sigCh)\n\t\t\tfmt.Printf("\\nReceived signal: %s\\n", recvSig)\n\t\t\treturn\n\t\t}\n\t}\n}\n<\/code><\/pre>\n
# User pressed Ctrl+C example\n$ go run .\nRunning forever until you either press Ctrl+C or `kill 35417`\n^C\nReceived signal: interrupt\n<\/code><\/pre>\n
# Process was killed\nterminal1$ go run .\nRunning forever until you either press Ctrl+C or `kill 35454`\n\nterminal2$ kill 35454\n\nterminal1$\n...\nsignal: terminated\n<\/code><\/pre>\n
Cobra<\/h3>\n