Created
December 27, 2021 00:55
-
-
Save paderinandrey/b21249cd3e35c6361398a43c1aff9cdf to your computer and use it in GitHub Desktop.
RDS-06
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"os" | |
"strconv" | |
"github.com/go-redis/redis/v8" | |
) | |
func main() { | |
var ctx = context.Background() | |
rdb := redis.NewClient(&redis.Options{ | |
Addr: "localhost:6379", | |
Password: "", | |
DB: 0, | |
}) | |
key := os.Args[1] | |
limit, err := strconv.Atoi(os.Args[2]) | |
if err != nil { | |
fmt.Println(err) | |
} | |
for i := 0; i < limit; i++ { | |
val := rdb.LPop(ctx, key).Val() | |
if val != "" { | |
fmt.Println(val) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"context" | |
"fmt" | |
"os" | |
"github.com/go-redis/redis/v8" | |
) | |
func main() { | |
var ctx = context.Background() | |
rdb := redis.NewClient(&redis.Options{ | |
Addr: "localhost:6379", | |
Password: "", | |
DB: 0, | |
}) | |
key := os.Args[1] | |
scanner := bufio.NewScanner(os.Stdin) | |
for scanner.Scan() { | |
if rdb.LLen(ctx, key).Val() == 20 { | |
rdb.LPop(ctx, key) | |
} | |
err := rdb.RPush(ctx, key, scanner.Text()).Err() | |
if err != nil { | |
fmt.Println(err) | |
} | |
} | |
if err := scanner.Err(); err != nil { | |
fmt.Fprintln(os.Stderr, "reading standard input:", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment