More on it here https://dave.cheney.net/2014/03/25/the-empty-struct.
It can be used with channels as well if you just want to notify whatever is listening on a channel that something happened and not to transfer data between goroutines.
// DO NOT DO THIS: (it uses more memory)
func removeDuplicateUsers(users []User) []User {
u := map[string]bool{}
cleanedUsers := []User{}
for _, user := range users {
if !u[user.ID] {
u[user.ID] = true
cleanedUsers = append(cleanedUsers, user)
}
}
return cleanedUsers
}
// DO THIS INSTEAD:
func removeDuplicateUsers(users []User) []User {
u := map[string]struct{}{}
cleanedUsers := []User{}
for _, user := range users {
if _, ok := u[user.ID]; !ok { // check if already found/exists
u[user.ID] = struct{}{}
cleanedUsers = append(cleanedUsers, user)
}
}
return cleanedUsers
}