乐闻世界logo
搜索文章和话题

What is the purpose of the defer keyword in Go?

1个答案

1

The defer keyword in Go is used to ensure that a function call is executed when the surrounding function returns. Specifically, the function following defer is executed when the surrounding function exits, regardless of whether it returns normally or exits early due to an error.

Main Uses

  1. Resource Cleanup: For example, closing file handles, database connections, and releasing locks.

  2. Error Handling: When handling errors, defer can be used to ensure that necessary cleanup logic is executed.

Examples

File Operation Example

go
func ReadFile(filename string) (string, error) { f, err := os.Open(filename) if err != nil { return "", err } defer f.Close() // Ensures the file is closed when the function exits contents, err := ioutil.ReadAll(f) if err != nil { return "", err } return string(contents), nil }

In this example, defer f.Close() ensures that the file is properly closed regardless of how the function exits (normal reading or an error occurs), thus avoiding resource leaks.

Lock Operation Example

go
func processData(data *Data) { mu.Lock() defer mu.Unlock() // Ensures the lock is released regardless of how processData exits // Process data }

In this example, defer mu.Unlock() ensures that the lock is released regardless of whether an error occurs or the function returns early during data processing, which is critical for avoiding deadlocks.

In this way, Go's defer keyword effectively simplifies error handling and resource management code, making it clearer and safer.

2024年8月7日 21:49 回复

你的答案