func getTeams()

in get-started-with-redis/pubsub.go [34:61]


func getTeams(client *redis.Client) []Team {
	ctx := context.Background()
	teams := make([]Team, 3)
	teamsets := make([]string, 0, 3)
	keys := make([]string, 0, 3)
	var cursor uint64
	for {
		// Scan returns a slice of matches. The count may or may not be reached
		// in the first call to Scan, so the code needs to call Scan in a loop and
		// append the found keys to the teamsets slice until the cursor "returns to 0".
		var err error
		keys, cursor, err = client.Scan(ctx, cursor, "team:*", 3).Result()
		if err != nil {
			break
		}
		teamsets = append(teamsets, keys...)
		if cursor == 0 {
			break
		}
	}
	// Lazily assume that the scan has returned 3 team sets
	for i := 0; i < 3; i++ {
		teams[i].name = teamsets[i]
		// each team uses its own client
		teams[i].client = newClient(dbconn, 0)
	}
	return teams
}