From a61260079ff9521196cd0bb6ddff76f935973113 Mon Sep 17 00:00:00 2001 From: Junv Date: Sat, 12 Oct 2019 16:50:15 +1100 Subject: [PATCH] Update go.md --- categories/go.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/categories/go.md b/categories/go.md index bc5e6e7..b0cba70 100644 --- a/categories/go.md +++ b/categories/go.md @@ -87,6 +87,23 @@ m.Store("a", "b") value, ok := m.Load("a") ``` +### Defer to Clean Up + +Use defer to clean up resources such as files and locks. +```go +p.Lock() +defer p.Unlock() + +if p.count < 10 { + return p.count +} + +p.count++ +return p.count + +// more readable +``` + # Frameworks ## db @@ -112,6 +129,8 @@ value, ok := m.Load("a") - test - Testify - Ginkgo +- Linter + - Golangci-lint https://github.com/golangci/golangci-lint # Tips @@ -124,7 +143,7 @@ value, ok := m.Load("a") ## map * implement by hash table -* slice can't be the key of a map, but sized array could. `var a map[[2]int]string` +* slice can't be the key of a map, but sized array could. e.g. `var a map[[2]int]string` ## Go has no generics