/
jeffs-basic-go-template.go
68 lines (52 loc) · 1.15 KB
/
jeffs-basic-go-template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"flag"
"fmt"
"os"
errors "github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
const toolVersion = "1.0.1"
var integerPtr *int
// A Function that also returns an error
func jeffFunc(x int) (string, error) {
if x == 42 {
// Make your error
return "error", errors.New("can't work with 42")
}
return "Everything worked great", nil
}
// Check your error
func checkErr(err error) {
if err != nil {
fmt.Printf("Error is %+v\n", err)
log.Fatal("ERROR:", err)
}
}
func init() {
// SET LOG LEVEL
// log.SetLevel(log.InfoLevel)
log.SetLevel(log.TraceLevel)
// SET FORMAT
log.SetFormatter(&log.TextFormatter{})
// log.SetFormatter(&log.JSONFormatter{})
// SET OUTPUT (DEFAULT stderr)
log.SetOutput(os.Stdout)
// VERSION FLAG
version := flag.Bool("v", false, "prints current version")
// INTEGER FLAG
integerPtr = flag.Int("i", 42, "This is the flag for an integer")
// Parse the flags
flag.Parse()
// CHECK VERSION
if *version {
fmt.Println(toolVersion)
os.Exit(0)
}
}
func main() {
log.Trace("Calling jeffFunc to get result")
r, err := jeffFunc(*integerPtr)
checkErr(err)
fmt.Println("Returned", r)
}