Skip to content

Commit 7d77efa

Browse files
overhaul Go code to make it more useful (#39)
1 parent 134cf34 commit 7d77efa

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

main.go

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"flag"
67
"fmt"
7-
"log"
88
"os"
99
"os/signal"
1010
"runtime/debug"
@@ -15,12 +15,6 @@ import (
1515

1616
const projectName = "Go Project Template" // REPLACE WITH YOUR PROJECT NAME HERE
1717

18-
var (
19-
debugLogs bool
20-
logPath string
21-
printVersion bool
22-
)
23-
2418
func usage() {
2519
fmt.Fprintf(os.Stderr, `
2620
<Project description>
@@ -39,23 +33,26 @@ For more information, see https://github.com/<user>/<repo>.
3933
`[1:])
4034
}
4135

42-
func init() {
43-
flag.Usage = usage
44-
flag.BoolVar(&debugLogs, "debug", false, "enable debug logging")
45-
flag.StringVar(&logPath, "l", "stdout", "path to log to")
46-
flag.BoolVar(&printVersion, "version", false, "print version and build information and exit")
47-
}
48-
4936
func main() {
5037
os.Exit(mainRetCode())
5138
}
5239

5340
func mainRetCode() int {
41+
var (
42+
debugLogs bool
43+
logPath string
44+
printVersion bool
45+
)
46+
47+
flag.Usage = usage
48+
flag.BoolVar(&debugLogs, "debug", false, "enable debug logging")
49+
flag.StringVar(&logPath, "l", "stdout", "path to log to")
50+
flag.BoolVar(&printVersion, "version", false, "print version and build information and exit")
5451
flag.Parse()
5552

5653
info, ok := debug.ReadBuildInfo()
5754
if !ok {
58-
log.Println("build information not found")
55+
fmt.Fprintln(os.Stderr, "build information not found")
5956
return 1
6057
}
6158

@@ -76,14 +73,10 @@ func mainRetCode() int {
7673

7774
logger, err := logCfg.Build()
7875
if err != nil {
79-
log.Printf("error creating logger: %v", err)
76+
fmt.Fprintf(os.Stderr, "error creating logger: %v", err)
8077
return 1
8178
}
8279

83-
// may also want to add syscall.SIGTERM on unix based OSes
84-
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
85-
defer cancel()
86-
8780
// log current version/commit
8881
versionFields := []zap.Field{
8982
zap.String("version", version),
@@ -96,12 +89,36 @@ func mainRetCode() int {
9689
}
9790
logger.Info("starting "+projectName, versionFields...)
9891

92+
// may also want to add golang.org/x/sys/unix.SIGTERM on unix based OSes
93+
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
94+
defer cancel()
95+
96+
if err := mainErr(ctx, logger); err != nil {
97+
var exitCode *errJustExit
98+
if errors.As(err, &exitCode) {
99+
return int(*exitCode)
100+
}
101+
fmt.Fprintln(os.Stderr, err)
102+
return 1
103+
}
104+
return 0
105+
}
106+
107+
type errJustExit int
108+
109+
func (e errJustExit) Error() string { return fmt.Sprintf("exit: %d", e) }
110+
111+
// this disables a linter warning because nil is unconditionally returned
112+
// here, remove this when adding your own code that can return errors
113+
//
114+
//nolint:unparam
115+
func mainErr(ctx context.Context, logger *zap.Logger) error {
99116
// START MAIN LOGIC HERE
100117

101118
<-ctx.Done()
102119
logger.Info("shutting down")
103120

104121
// STOP MAIN LOGIC HERE
105122

106-
return 0
123+
return nil
107124
}

0 commit comments

Comments
 (0)