Skip to content

Instantly share code, notes, and snippets.

@narqo
Last active July 27, 2020 19:56
Show Gist options
  • Save narqo/e65a5495277ec6a035de15160076ef49 to your computer and use it in GitHub Desktop.
Save narqo/e65a5495277ec6a035de15160076ef49 to your computer and use it in GitHub Desktop.
A boilerplate for a new Go application
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