golang log.Fatal() 和 panic() 函数的区别

本文详细对比了Go语言中log.Fatal与os.Exit函数的功能。log.Fatal用于打印错误并退出程序,同时不会执行defer函数;而os.Exit直接终止程序,同样不执行defer函数。此外,文章还介绍了panic函数,其在触发时会执行所有defer函数,并在没有被捕获的情况下终止程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在讲两者区别之前我们先看一下os.Exit()函数的定义:

func Exit(code int)

Exit causes the current program to exit with the given status code.
Conventionally, code zero indicates success, non-zero an error.
The program terminates immediately; deferred functions are not run.

注意两点:

  1. 应用程序马上退出。
  2. defer函数不会执行。

再来看log.Fatal函数定义

func Fatal(v ...interface{})

Fatal is equivalent to Print() followed by a call to os.Exit(1).

看源代码:go/src/log/log.go

// Fatal is equivalent to l.Print() followed by a call to os.Exit(1).
func (l *Logger) Fatal(v ...interface{}) {
    l.Output(2, fmt.Sprint(v...))
    os.Exit(1)
}

总结起来log.Fatal函数完成:

  1. 打印输出内容
  2. 退出应用程序
  3. defer函数不会执行

和os.Exit()相比多了第一步。

再来看内置函数panic()函数定义:

// The panic built-in function stops normal execution of the current
// goroutine. When a function F calls panic, normal execution of F stops
// immediately. Any functions whose execution was deferred by F are run in
// the usual way, and then F returns to its caller. To the caller G, the
// invocation of F then behaves like a call to panic, terminating G's
// execution and running any deferred functions. This continues until all
// functions in the executing goroutine have stopped, in reverse order. At
// that point, the program is terminated and the error condition is reported,
// including the value of the argument to panic. This termination sequence
// is called panicking and can be controlled by the built-in function
// recover.
func panic(v interface{})

注意几点:

  1. 函数立刻停止执行 (注意是函数本身,不是应用程序停止)
  2. defer函数被执行
  3. 返回给调用者(caller)
  4. 调用者函数假装也收到了一个panic函数,从而
    4.1 立即停止执行当前函数
    4.2 它defer函数被执行
    4.3 返回给它的调用者(caller)
  5. ...(递归重复上述步骤,直到最上层函数)
    应用程序停止。
  6. panic的行为

简单的总结panic()就有点类似java语言的exception的处理,因而panic的行为和java的exception处理行为就非常类似,行为结合catch,和final语句块的处理流程。

下面给几个例子:

例子1:log.Fatal

package main

import (
    "log"
)

func foo() {
    defer func () { log.Print("3333")} ()
    log.Fatal("4444")
}

func main() {
    log.Print("1111")
    defer func () { log.Print("2222")} ()
    foo()
    log.Print("9999")
}

运行结果:

$ go build && ./main
2018/08/20 17:48:44 1111
2018/08/20 17:48:44 4444

可见defer函数的内容并没有被执行,程序在log.Fatal(...)处直接就退出了。

例子2:panic()函数

package main

import (
    "log"
)

func foo() {
    defer func () { log.Print("3333")} ()
    panic("4444")
}

func main() {
    log.Print("1111")
    defer func () { log.Print("2222")} ()
    foo()
    log.Print("9999")
}

运行结果:

$ go build && ./main
2018/08/20 17:49:28 1111
2018/08/20 17:49:28 3333
2018/08/20 17:49:28 2222
panic: 4444

goroutine 1 [running]:
main.foo()
        /home/.../main.go:9 +0x55
main.main()
        /home/.../main.go:15 +0x82

可见所有的defer都被调用到了,函数根据父子调用关系所有的defer都被调用直到最上层。
当然如果其中某一层函数定义了recover()功能,那么panic会在那一层函数里面被截获,然后由recover()定义如何处理这个panic,是丢弃,还是向上再抛出。(是不是和exception的处理机制一模一样呢?)



 

package main import ( "fmt" "log" "os" "time" "github.com/goburrow/modbus" "github.com/tarm/serial" MQTT "github.com/eclipse/paho.mqtt.golang" ) // 定义回调函数,处理订阅的消息 var f MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) { fmt.Printf("TOPIC: %s\n", msg.Topic()) fmt.Printf("MSG: %s\n", msg.Payload()) } func main() { // 创建新的客户端 opts := MQTT.NewClientOptions().AddBroker("tcp://localhost:1883") opts.SetClientID("golang-client") opts.SetDefaultPublishHandler(f) c := MQTT.NewClient(opts) if token := c.Connect(); token.Wait() && token.Error() != nil { panic(token.Error()) } // 订阅主题 if token := c.Subscribe("golang/topic", 0, nil); token.Wait() && token.Error() != nil { fmt.Println(token.Error()) os.Exit(1) } // 连接串口 c := &serial.Config{Name: "/dev/ttyS0", Baud: 115200} s, err := serial.OpenPort(c) if err != nil { log.Fatal(err) } // 创建 Modbus 从机实例 handler := modbus.NewRTUClientHandler(s) handler.BaudRate = 115200 handler.DataBits = 8 handler.Parity = "N" handler.StopBits = 1 handler.SlaveId = 1 handler.Timeout = 5 * time.Second defer handler.Close() client := modbus.NewClient(handler) // 读取寄存器 results, err := client.ReadHoldingRegisters(1, 2) if err != nil { log.Fatal(err) } fmt.Println(results) // 输出读取到的寄存器值 // 设置寄存器 err = client.WriteMultipleRegisters(1, 2, []byte{0x01, 0x02}) if err != nil { log.Fatal(err) } // 发布消息 for i := 0; i < 5; i++ { text := fmt.Sprintf("this is msg #%d!", i) token := c.Publish("golang/topic", 0, false, text) token.Wait() } time.Sleep(3 * time.Second) // 断开连接 if token := c.Unsubscribe("golang/topic"); token.Wait() && token.Error() != nil { fmt.Println(token.Error()) os.Exit(1) } c.Disconnect(250) }
02-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值