From 94706e8468e3226b8984965da0cac10b6e62914a Mon Sep 17 00:00:00 2001 From: hugmouse Date: Fri, 29 Oct 2021 17:00:19 +0800 Subject: [PATCH] Use zero-allocation approach for empty slices For further reading checkout https://github.com/golang/go/wiki/CodeReviewComments#declaring-empty-slices --- hyphae/iterators.go | 2 +- user/files.go | 7 +++++-- user/users.go | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/hyphae/iterators.go b/hyphae/iterators.go index e2d6749..ca89d08 100644 --- a/hyphae/iterators.go +++ b/hyphae/iterators.go @@ -79,7 +79,7 @@ func PathographicSort(src chan string) <-chan string { // Subhyphae returns slice of subhyphae. func (h *Hypha) Subhyphae() []*Hypha { - hyphae := []*Hypha{} + var hyphae []*Hypha for subh := range YieldExistingHyphae() { if strings.HasPrefix(subh.Name, h.Name+"/") { hyphae = append(hyphae, subh) diff --git a/user/files.go b/user/files.go index 2f98207..1ad45d7 100644 --- a/user/files.go +++ b/user/files.go @@ -82,7 +82,7 @@ func SaveUserDatabase() error { } func dumpUserCredentials() error { - userList := []*User{} + var userList []*User // TODO: lock the map during saving to prevent corruption for u := range YieldUsers() { @@ -119,5 +119,8 @@ func dumpTokens() { log.Println(err) return } - os.WriteFile(files.TokensJSON(), blob, 0666) + err = os.WriteFile(files.TokensJSON(), blob, 0666) + if err != nil { + log.Println("an error occurred in dumpTokens function:", err) + } } diff --git a/user/users.go b/user/users.go index 07fe2a9..fadc18a 100644 --- a/user/users.go +++ b/user/users.go @@ -20,7 +20,7 @@ func YieldUsers() chan *User { // ListUsersWithGroup returns a slice with users of desired group. func ListUsersWithGroup(group string) []string { - filtered := []string{} + var filtered []string for u := range YieldUsers() { if u.Group == group { filtered = append(filtered, u.Name)