package main import "os" func main() { os.Exit(1) } //结果: 进程 已完成,退出代码为 1 package main import "os..." func main() { os.Exit(0) } //结果: 进程 已完成,退出代码为 0 2.log.Fatal() package main import "log" func main...,但是会打印里面的内容,之前的 defer 函数不会运行 log.Fatal()的源码 // Fatal is equivalent to Print() followed by a call to os.Exit...os.Exit(1) } 3.panic() panic 内置函数停止当前 goroutine 的正常执行。当函数 F 调用 panic 时,F 的正常执行会立即停止。
/install.lock file to reinstall")os.Exit(1)}// Verify required files existsqlFile := "import.sql"dataExists...= nil {log.Printf("Failed to read SQL file %s: %v\n", sqlFile, err)os.Exit(1)}sqlArr := strings.Split...\nError: %v\n", sql, err)log.Println("Database initialization failed - please check SQL statements")os.Exit...= nil {log.Printf("Failed to create lock file: %v\n", err)os.Exit(1)}defer installFile.Close()_, err...= nil {log.Printf("Failed to write lock file: %v\n", err)os.Exit(1)}log.Println("Database initialization
}c = codec.AvcodecAllocContext3()if c == nil {fmt.Printf("Could not allocate audio codec context\n")os.Exit...0 {fmt.Printf("Encoder does not support sample format %s",libavutil.AvGetSampleFmtName(c.SampleFmt))os.Exit...c.ChannelLayout)/* open it */if c.AvcodecOpen2(codec, nil) os.Exit...(1)}f, _ = os.Create(filename)if f == nil {fmt.Printf("Could not open %s\n", filename)os.Exit(1)}/* packet...a copy if the encoder * kept a reference internally */ret = frame.AvFrameMakeWritable()if ret os.Exit
/install.lock file to reinstall") os.Exit(1) } // Verify required files exist sqlFile...= nil { log.Printf("Failed to read SQL file %s: %v\n", sqlFile, err) os.Exit(1) }...log.Println("Database initialization failed - please check SQL statements") os.Exit...= nil { log.Printf("Failed to create lock file: %v\n", err) os.Exit(1) } defer...= nil { log.Printf("Failed to write lock file: %v\n", err) os.Exit(1) } log.Println
= nil { fmt.Println("The connection failed") os.Exit(1) } defer ftpClient.Quit...= nil { fmt.Println("The login failed") os.Exit(1) } //Print the current...= nil { fmt.Println("The Pwd command failed") os.Exit(1) } fmt.Println("The...= nil { fmt.Println("The Pwd command failed") os.Exit(1) } fmt.Println("size
= nil { fmt.Println(err) os.Exit(0) } for i := 0; i < 5; i++ { file.WriteString...= nil { fmt.Println(err) os.Exit(0) } buf := make([]byte, 1024) for {...= nil { fmt.Println(del) os.Exit(0) } }() }
= nil { os.Stderr.WriteString(err.Error()) os.Stderr.WriteString("\n") os.Exit...(1) } defer resp.Body.Close() io.Copy(os.Stdout, resp.Body) os.Exit(0) } func getinternal...= nil { os.Stderr.WriteString("Oops:" + err.Error()) os.Exit(1) } for , a :=...{ os.Stdout.WriteString(ipnet.IP.String() + "\n") } } } os.Exit
= nil { fmt.Printf("Could not find or create folder: %s\n", err) os.Exit(1)}if _, err := client.UpsertDashboard...= nil { fmt.Printf("Could not create dashboard: %s\n", err) os.Exit(1)} 当然官方还提供了一个比较完整的 example...= nil { fmt.Fprintf(os.Stderr, "Could not parse file: %s\n", err) os.Exit(1)}ctx := context.Background...= nil { fmt.Printf("Could not find or create folder: %s\n", err) os.Exit(1)}if _, err := client.UpsertDashboard...= nil { fmt.Printf("Could not create dashboard: %s\n", err) os.Exit(1)} 同样也可以找到比较完整的 example[5]
= nil { fmt.Printf("Error listing deployments: %v\n", err) fmt.Println("非0") os.Exit...= nil { fmt.Printf("Error listing statefulsets: %v\n", err) fmt.Println("非0") os.Exit...= nil { fmt.Printf("Error listing deployments: %v\n", err) fmt.Println("非0") os.Exit...= nil { fmt.Printf("Error building config: %v\n", err) fmt.Println("非0") os.Exit...= nil { fmt.Printf("Error creating clientset: %v\n", err) fmt.Println("非0") os.Exit
使用os.Exit立即以给定的状态退出。...package mainimport ( "fmt" "os")func main() {// 使用os.Exit时,defer不会被执行,因此这个fmt.Println永远不会被调用。...os.Exit(3)}运行结果:➜ go run exit.go exit status 3
= nil { fmt.Fprintf(os.Stderr, "failed parsing config: %v\n", err) os.Exit(1) }...if config.PrintVersion { fmt.Println(version.Print("loki")) os.Exit(0) } //...{ level.Error(util_log.Logger).Log("msg", "validating config", "err", err.Error()) os.Exit...if config.VerifyConfig { level.Info(util_log.Logger).Log("msg", "config is valid") os.Exit...initialising loki", err, util_log.Logger) if config.ListTargets { t.ListTargets() os.Exit
/* open it */ if c.AvcodecOpen2(codec, nil) < 0 { fmt.Printf("Could not open codec\n") os.Exit...} f, _ = os.Create(filename) if f == nil { fmt.Printf("Could not open %s\n", filename) os.Exit...libavcodec.AvPacketAlloc() if pkt == nil { fmt.Printf("could not allocate the packet\n") os.Exit...libavutil.AvFrameAlloc() if frame == nil { fmt.Printf("Could not allocate audio frame\n") os.Exit...* kept a reference internally */ ret = frame.AvFrameMakeWritable() if ret < 0 { os.Exit
\n", os.Args[0])os.Exit(0)}filename = os.Args[1]outfilename = os.Args[2]pkt = libavcodec.AvPacketAlloc...()if pkt == nil {os.Exit(1)}/* set end of buffer to 0 (this ensures that no overreading happens for damaged...}parser = libavcodec.AvParserInit(int32(codec.Id))if parser == nil {fmt.Printf("parser not found\n")os.Exit...= nil {fmt.Printf("Could not open %s,err = %s\n", filename, err)os.Exit(1)}frame = libavutil.AvFrameAlloc...()if frame == nil {fmt.Printf("Could not allocate video frame\n")os.Exit(1)}for {/* read raw data from
scanner.exe IP startport endport") fmt.Println("Endport must be larger than startport") os.Exit...scanner IP startport endport") fmt.Println("Endport must be larger than startport") os.Exit...fmt.Println("The scan process has spent ",endtime-starttime,"second") os.Exit
os.Exit(1) } func Fatalf(format string, v ...interface{}) { logger.Output(LevelFatal, fmt.Sprintf(format...os.Exit(1) } func Error(v ...interface{}) { p(LevelError, v...) } func Errorf(format string, v ......logger.Output(LevelInfo, s+" ok") } else { logger.Output(LevelFatal, fmt.Sprintf("%s %+v", s, err)) os.Exit..._log.Output(3, s) } func (l *logManager) Fatal(s string) { l.Output(LevelFatal, s) os.Exit(1) } func...os.Exit(1) } func (l *logManager) Error(s string) { l.Output(LevelError, s) } func (l *logManager)
(1) } pkt = libavcodec.AvPacketAlloc() if pkt == nil { os.Exit(1) } /* put sample parameters...0) } /* open it */ if c.AvcodecOpen2(codec, nil) < 0 { fmt.Printf("Could not open codec\n") os.Exit...(1) } f, _ = os.Create(filename) if f == nil { fmt.Printf("Could not open %s\n", filename) os.Exit...frame.AvFrameGetBuffer(0) if ret < 0 { fmt.Printf("Could not allocate the video frame data\n") os.Exit.../* make sure the frame data is writable */ ret = frame.AvFrameMakeWritable() if ret < 0 { os.Exit
= nil { fmt.Println("Error when listen: ", remote) os.Exit(-1) } for {...= nil { fmt.Println("Error accepting client: ", err.Error()) os.Exit(0)...os.Exit(-1) } fmt.Println("Connection OK.")...os.Exit(1) } in, err := con.Write([]byte(str)) if err !...= nil { fmt.Printf("Error when send to server: %d\n", in) os.Exit(0)
0var fmt0 stringif len(os.Args) \n", os.Args[0])os.Exit...}parser = libavcodec.AvParserInit(int32(codec.Id))if parser == nil {fmt.Printf("Parser not found\n")os.Exit...}c = codec.AvcodecAllocContext3()if c == nil {fmt.Printf("Could not allocate audio codec context\n")os.Exit...(1)}/* open it */if c.AvcodecOpen2(codec, nil) os.Exit(1)}var...= nil {fmt.Printf("Could not open %s\n", filename)os.Exit(1)}outfile, err = os.Create(outfilename)if
< 0 { fmt.Printf("Error encoding a frame: %s\n", libavutil.AvErr2str(ret)) os.Exit...ret < 0 { fmt.Printf("Could not open audio codec: %s\n", libavutil.AvErr2str(ret)) os.Exit...() if ost.swr_ctx == nil { fmt.Printf("Could not allocate resampler context\n") os.Exit...it here */ ret = ost.frame.AvFrameMakeWritable() if ret < 0 { os.Exit...\n") os.Exit(1) } return picture } func open_video(oc *libavformat.AVFormatContext,
os.Exit(1) } file.Close() cfg, err := ini.Load("/tmp/myga.ini") if err !...os.Exit(1) } file.Close() cfg, err := ini.Load("/tmp/myga.ini") if err !...os.Exit(1) } file.Close() cfg, err := ini.Load("/tmp/myga.ini") if err !...os.Exit(1) } cfg.DeleteSection(item) cfg.SaveTo("/tmp/myga.ini") fmt.Println("ok!!!"...os.Exit(1) } cfg.Section(item).Key("secret").SetValue(secret) cfg.SaveTo("/tmp/myga.ini"