Skip to content

Instantly share code, notes, and snippets.

@dragonsinth
Created February 14, 2020 22:08
Show Gist options
  • Save dragonsinth/cb7fbf38efbaf3a293eee32d46e81a36 to your computer and use it in GitHub Desktop.
Save dragonsinth/cb7fbf38efbaf3a293eee32d46e81a36 to your computer and use it in GitHub Desktop.
func GetFriends(ctx context.Context, user int64) (map[string]*User, error) {
// Produce
var friendIds []int64
for it := GetFriendIds(user); ; {
if id, err := it.Next(ctx); err != nil {
if err == io.EOF {
break
}
return nil, fmt.Errorf("GetFriendIds %d: %w", user, err)
} else {
friendIds = append(friendIds, id)
}
}
// Map
ret := map[string]*User{}
for _, friendId := range friendIds {
if friend, err := GetUserProfile(ctx, friendId); err != nil {
return nil, fmt.Errorf("GetUserProfile %d: %w", friendId, err)
} else {
ret[friend.Name] = friend
}
}
return ret, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment