gorm - best orm for Golang out there. Saves a lot of dev time and quite efficient
gorilla/mux - best request router and dispatcher for Golang
Top N players
Algorithm GetLeaderBoard(n)
Input: n (integer) - the number of top players to retrieve
// Check if the requested number of players (n) exceeds the limit in Redis
if n > limit:
// Fetch the top n players from the database
players, err := getNPlayers(n)
// If there's an error fetching players from the database, return the error
if err is not nil:
return nil, err
// Attempt to set the fetched players in the Redis leaderboard
err = setLeaderboardToRedis(players)
// Log the error if setting data in Redis fails, but continue execution
if err is not nil:
log_error("error writing leaderboard to redis", err)
// Update the limit and last player based on the fetched players
limit = length of players
last = last element of players
// Return the fetched players as the leaderboard
return players, nil
end if
// If n does not exceed the limit in Redis, attempt to load players from Redis
players, err := loadLeaderboardFromRedis()
// If there's an error loading data from Redis, return the error
if err is not nil:
return nil, err
// If there are players loaded from Redis
if length of players > 0:
// Return the top n players from the loaded data
return players[:n], nil
// If there are no players loaded from Redis, return nil
return nil, nil
End Algorithm