Last active
July 27, 2020 19:56
-
-
Save narqo/e65a5495277ec6a035de15160076ef49 to your computer and use it in GitHub Desktop.
A boilerplate for a new Go application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"log" | |
"os" | |
"os/signal" | |
"syscall" | |
) | |
func main() { | |
ctx, cancel := context.WithCancel(context.Background()) | |
defer cancel() | |
sigc := make(chan os.Signal, 2) | |
signal.Notify(sigc, os.Interrupt, syscall.SIGTERM) | |
go func() { | |
<-sigc | |
signal.Stop(sigc) | |
cancel() | |
}() | |
if err := run(ctx, os.Args[1:]); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func run(ctx context.Context, args []string) error { | |
flags := flag.NewFlagSet("", flag.ExitOnError) | |
if err := flags.Parse(args); err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment