Created
January 28, 2017 08:36
-
-
Save cipepser/c953b981cec15f23ef604cf03f473e38 to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"os" | |
"bufio" | |
"io" | |
"log" | |
"strconv" | |
) | |
func main() { | |
rfp, err := os.Open(os.Args[1]) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer rfp.Close() | |
N, err := strconv.Atoi(os.Args[2]) | |
if err != nil { | |
log.Fatal(err) | |
} | |
reader := bufio.NewReaderSize(rfp, 4096) | |
var count int | |
output := make([][]byte, N) | |
for { | |
p, _, err := reader.ReadLine() | |
count++ | |
if err == io.EOF { | |
break | |
} | |
output[(count-1) % N] = p | |
} | |
if count < N { | |
output = output[:count - 1] | |
} | |
i := (count - 1) % N | |
output = append(output[i:], output[:i]...) | |
for _, v := range output { | |
fmt.Println(string(v)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment