calls to log.Fatal only in main() or init() functions
255 buf := []byte(fdContent)
256 _, err = fd.Write(buf)
257 if err != nil {
258 log.Fatal(err)259 }
260}
261
calls to log.Fatal only in main() or init() functions
239 // Output csv file
240 fd, err := os.OpenFile("result.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
241 if err != nil {
242 log.Fatal(err)243 }
244 defer func() {
245 if err := fd.Close(); err != nil {
calls to log.Fatal only in main() or init() functions
97 }
98 delete(c.regionMap, regionID)
99 } else {
100 log.Fatal("error when add sourceStore in transfer region map", zap.Uint64("source-store", sourceStoreID), zap.Uint64("region", regionID))101 }
102}
103
calls to log.Fatal only in main() or init() functions
134 log.Fatal(err)
135 }
136 } else {
137 log.Fatal("unsupported time layout")138 }
139 return func(content string) (time.Time, error) {
140 result := r.FindStringSubmatch(content)
calls to log.Fatal only in main() or init() functions
131 if pattern, ok := supportLayouts[layout]; ok {
132 r, err = regexp.Compile(pattern)
133 if err != nil {
134 log.Fatal(err)135 }
136 } else {
137 log.Fatal("unsupported time layout")
calls to log.Fatal only in main() or init() functions
58func MustParseVersion(v string) *semver.Version {
59 ver, err := ParseVersion(v)
60 if err != nil {
61 log.Fatal("version string is illegal", errs.ZapError(err)) 62 }
63 return ver
64}
calls to log.Fatal only in main() or init() functions
66func MinSupportedVersion(v Feature) *semver.Version {
67 target, ok := featuresDict[v]
68 if !ok {
69 log.Fatal("the corresponding version of the feature doesn't exist", zap.Int("feature-number", int(v)), errs.ZapError(errs.ErrFeatureNotExisted))70 }
71 version := MustParseVersion(target)
72 return version
calls to log.Fatal only in main() or init() functions
49 addr := fmt.Sprintf("http://%s", l.Addr())
50 err = l.Close()
51 if err != nil {
52 log.Fatal("close failed", errs.ZapError(err))53 }
54
55 testAddrMutex.Lock()
calls to log.Fatal only in main() or init() functions
44func tryAllocTestURL() string {
45 l, err := net.Listen("tcp", "127.0.0.1:0")
46 if err != nil {
47 log.Fatal("listen failed", errs.ZapError(err))48 }
49 addr := fmt.Sprintf("http://%s", l.Addr())
50 err = l.Close()
calls to log.Fatal only in main() or init() functions
37 }
38 time.Sleep(time.Second)
39 }
40 log.Fatal("failed to alloc test URL")41 return ""
42}
43
calls to log.Fatal only in main() or init() functions
84// Commonly used with a `defer`.
85func LogPanic() {
86 if e := recover(); e != nil {
87 log.Fatal("panic", zap.Reflect("recover", e)) 88 }
89}
90
calls to log.Fatal only in main() or init() functions
154
155func mustNil(err error) {
156 if err != nil {
157 log.Fatal("unexpected error", zap.Error(err), zap.Stack("stack"))158 }
159}
160
calls to log.Fatal only in main() or init() functions
46 resPath := MustGetResPath()
47 strings, err := distro.ReadResourceStringsFromFile(path.Join(resPath, stringsFileName))
48 if err != nil {
49 log.Fatal("failed to load distro strings", zap.Error(err))50 }
51 log.Info("using distribution strings", zap.Any("strings", strings))
52 distro.ReplaceGlobal(strings)
calls to log.Fatal only in main() or init() functions
33func MustGetResPath() string {
34 exePath, err := os.Executable()
35 if err != nil {
36 log.Fatal("failed to read the execution path", zap.Error(err))37 return ""
38 }
39 return path.Join(path.Dir(exePath), resFolderName)
Description
Packages exposing functions that can stop program execution by exiting are hard to reuse. Program exits in functions other than main() or init() are not encouraged.
Bad practice
func nonMainFunc() {
safelyExit := true // somehow arrive at this value
if safelyExit {
os.Exit(0)
}
}
func main() {
nonMainFunc()
}
Recommended
func nonMainFunc() bool {
safelyExit := true // somehow arrive at this value
}
func main() {
safelyExit := nonMainFunc()
if safelyExit {
os.Exit(0)
}
}