📖 100 Go Mistakes and How to Avoid Them
Find a file
Philipp Stehle f807004a97
Update #47: change function call order in closure variant
As defer statements are evaluate in reverse order simply wrapping multiple defer in a closure reverses their call order.

Sometimes this might be irrelevant, but as a general advise, I would recommend to swap it.

See https://play.golang.com/p/NTtl-mHnx2x

```golang
package main

import (
	"fmt"
)

func main() {
	direct()
	closure()
}

func direct() {
	defer fmt.Println("direct a")
	defer fmt.Println("direct b")

}

func closure() {
	defer func() {
		fmt.Println("closure a")
		fmt.Println("closure b")
	}()
}

/* Output:
direct b
direct a
closure a
closure b
*/
```

Another difference is behavior on `panic`: When adding two separate `defers` and one panics the other is still executed. This is not the case for a single defered closure. See https://play.golang.com/p/cMYJ7c3pVYJ
2024-08-12 22:30:16 +02:00
.cache/plugin/social Brazilian portugese version 2024-04-05 23:09:57 +02:00
.github Fixing CI 2023-09-18 00:40:43 +02:00
docs Update #47: change function call order in closure variant 2024-08-12 22:30:16 +02:00
includes Abbreviation, structure 2024-03-08 00:40:56 +01:00
overrides Analytics 2024-03-15 21:22:55 +01:00
site Brazilian portugese version 2024-04-05 23:09:57 +02:00
src Add comments to indicate it's a bad/good example 2024-07-15 14:55:19 -04:00
.gitattributes Update .gitattributes 2023-10-01 19:29:14 +02:00
.gitignore init 2020-12-12 11:02:16 +01:00
CONTRIBUTING.md Create CONTRIBUTING.md 2023-10-01 13:27:11 +02:00
go.mod All 2021-12-27 15:56:17 +01:00
go.sum All 2021-12-27 15:56:17 +01:00
justfile Japanese, content 2023-10-10 19:22:17 +02:00
LICENSE.md Add license 2021-12-27 16:02:21 +01:00
mkdocs.yml Brazilian portugese version 2024-04-05 23:09:57 +02:00
README.md Automated 2023-09-14 10:41:08 +01:00

100 Go Mistakes and How to Avoid Them

Source code and community space of 📖 100 Go Mistakes and How to Avoid Them, published by Manning in August 2022.

Read online: 100go.co.