1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-08-06 22:04:11 +00:00

Use zero-allocation approach for empty slices

For further reading checkout https://github.com/golang/go/wiki/CodeReviewComments#declaring-empty-slices
This commit is contained in:
hugmouse 2021-10-29 17:00:19 +08:00 committed by handlerug
parent b2789f923f
commit 94706e8468
3 changed files with 7 additions and 4 deletions

View File

@ -79,7 +79,7 @@ func PathographicSort(src chan string) <-chan string {
// Subhyphae returns slice of subhyphae. // Subhyphae returns slice of subhyphae.
func (h *Hypha) Subhyphae() []*Hypha { func (h *Hypha) Subhyphae() []*Hypha {
hyphae := []*Hypha{} var hyphae []*Hypha
for subh := range YieldExistingHyphae() { for subh := range YieldExistingHyphae() {
if strings.HasPrefix(subh.Name, h.Name+"/") { if strings.HasPrefix(subh.Name, h.Name+"/") {
hyphae = append(hyphae, subh) hyphae = append(hyphae, subh)

View File

@ -82,7 +82,7 @@ func SaveUserDatabase() error {
} }
func dumpUserCredentials() error { func dumpUserCredentials() error {
userList := []*User{} var userList []*User
// TODO: lock the map during saving to prevent corruption // TODO: lock the map during saving to prevent corruption
for u := range YieldUsers() { for u := range YieldUsers() {
@ -119,5 +119,8 @@ func dumpTokens() {
log.Println(err) log.Println(err)
return 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)
}
} }

View File

@ -20,7 +20,7 @@ func YieldUsers() chan *User {
// ListUsersWithGroup returns a slice with users of desired group. // ListUsersWithGroup returns a slice with users of desired group.
func ListUsersWithGroup(group string) []string { func ListUsersWithGroup(group string) []string {
filtered := []string{} var filtered []string
for u := range YieldUsers() { for u := range YieldUsers() {
if u.Group == group { if u.Group == group {
filtered = append(filtered, u.Name) filtered = append(filtered, u.Name)