Delayed execution, infinite magic, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the defer keyword delay invocation mechanism in Golang uses EP17

Original link: https://v3u.cn/a_id_247

Define first, execute later. I have to admire the genius design of Golang designers. In fact, the defer keyword is equivalent to the finally syntax block in the try{…}except{…}finally{…} structural design in Python, the code logic that is enforced at the end of the function , but defer is more elegant in grammatical structure. It is executed uniformly before the function exits. The defer statement can be added at any time, which is mostly used for the release of system resources and related aftermath work. Of course, this process structure is necessary and can be different in form, but the underlying principles are similar. Golang chooses a more concise defer to avoid the multi-level nested tryexceptfinally structure. Operating system resources are unavoidable in business, such as singleton object usage rights, file read and write, database read and write, …

This article is reprinted from: https://v3u.cn/a_id_247
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment