search
HomeBackend DevelopmentPython TutorialDetailed introduction to the key features of defer in golang

Detailed introduction to the key features of defer in golang

Aug 12, 2017 pm 02:41 PM
defergolangThe essential

defer is a keyword in the golang language, which is used to release resources and will be called before the function returns. The following article mainly introduces you to the key features of defer in golang. The article introduces it in detail through sample code, which has certain reference and learning value for everyone. Friends who need it can take a look below.

Preface

Everyone knows golang’s defer keyword, which can perform some operations before the function returns. The most commonly used one is to open a When using a resource (such as a file, database connection, etc.), use defer to delay closing the resource to avoid memory leaks. This article mainly introduces to you the key features of defer in golang, and shares it for your reference and learning. Without further ado, let’s take a look at the detailed introduction:

1. defer The role and execution timing

go's defer statement is used to delay the execution of the function, and the delay occurs after calling the function return, such as

## The execution of
#

func a() int {
 defer b()
 return 0
}

b occurs after return 0. Pay attention to the syntax of defer. The keyword defer is followed by the function call.

2. Important uses of defer 1: Cleaning up and releasing resources

Due to the delay characteristics of defer, defer is often used at the end of a function call Then clean up related resources, such as


f, _ := os.Open(filename)
defer f.Close()

The release of file resources will be automatically executed with the help of defer after the function call is completed. There is no need to always remember where the resources need to be released. Open must correspond to release.

Use an example to deeply explain the convenience and simplicity brought by defer.

The main purpose of the code is to open a file and then copy the content to another new file. If there is no defer, write like this:


func CopyFile(dstName, srcName string) (written int64, err error) {
 src, err := os.Open(srcName)
 if err != nil {
  return
 }
 dst, err := os.Create(dstName)
 if err != nil { //1
  return
 }
 written, err = io.Copy(dst, src)
 dst.Close()
 src.Close()
 return
}

The code is in After returning at #1, the src file is not closed, which may result in resources not being released correctly. Use defer instead:


func CopyFile(dstName, srcName string) (written int64, err error) {
 src, err := os.Open(srcName)
 if err != nil {
  return
 }
 defer src.Close()
 dst, err := os.Create(dstName)
 if err != nil {
  return
 }
 defer dst.Close()
 return io.Copy(dst, src)
}

Both src and dst can be cleaned up and processed in time. Release, no matter where return is executed.

In view of the role of defer, defer is often used to release database connections, file open handles and other operations to release resources.

3. Important uses of defer 2: execute recovery

The defer function is executed after return. This is the right time. Panic thrown by a function can be captured, so another important use of defer is to perform recovery.

recover is only more meaningful when used in defer. If used elsewhere, the error cannot be effectively captured because the program has already been called and returns early.


package main
import (
 "fmt"
)
func main() {
 defer func() {
  if ok := recover(); ok != nil {
   fmt.Println("recover")
  }
 }()
 panic("error")
}

Remember defer should be placed before panic execution.

4. The execution sequence of multiple defers

The function of defer is to push the function execution after the keyword into a stack Medium delayed execution, the execution order of multiple defers is last-in-first-out LIFO:


defer func() { fmt.Println("1") }()
defer func() { fmt.Println("2") }()
defer func() { fmt.Println("3") }()

The output order is 321.

This feature can implement reverse order operations on an array.

5. The parameters of the deferred function are determined when defer

This is the characteristic of defer. When a function is defer, Its parameters are calculated and determined when deferred. Even if the parameters are modified after defer, it will have no effect on the function that has been defered. What does this mean? Look at the example:


func a() {
 i := 0
 defer fmt.Println(i)
 i++
 return
}

a The execution output is 0 instead of 1, because when defer, the value of i is 0, and the function parameters being defer have already been executed and calculated. and confirmed.

Look at another example:


func calc(index string, a, b int) int {
 ret := a + b
 fmt.Println(index, a, b, ret)
 return ret
}
func main() {
 a := 1
 b := 2
 defer calc("1", a, calc("10", a, b))
 a = 0
 return
}

Execution code output


10 1 2 3 
1 1 3 4

defer function parameters The third parameter has been calculated and determined when deferred, and the same is true for the second parameter a. It does not affect whether the a variable is modified later.

6. The defer function can read and modify the return value with name


func c() (i int) {
 defer func() { i++ }()
 return 1
}

The defer function is executed after return, and the return value with name can be modified. The above function c returns 2.

The above is the detailed content of Detailed introduction to the key features of defer in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Python and C  : Finding the Right ToolPython and C : Finding the Right ToolApr 19, 2025 am 12:04 AM

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python for Data Science and Machine LearningPython for Data Science and Machine LearningApr 19, 2025 am 12:02 AM

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools