Created
November 6, 2017 23:25
-
-
Save valdergallo/d45d2efc8cc68f8d5a6ae62f5f1a4545 to your computer and use it in GitHub Desktop.
TwiterStorm Dracula Book
This file contains hidden or 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" | |
"fmt" | |
"log" | |
"os" | |
"time" | |
) | |
const TailString string = "..." | |
const MaxTwitterLen int = 145 - len(TailString) | |
var iBuffer []string | |
func main() { | |
start := time.Now() | |
file, _ := os.Open("dracula.txt") | |
defer file.Close() | |
scanner := bufio.NewScanner(file) | |
content := "" | |
restContent := "" | |
counter := 1 | |
for scanner.Scan() { | |
// first line | |
if content == "" { | |
content += fmt.Sprint(counter) + ": " + restContent + scanner.Text() | |
} else { | |
content += restContent + scanner.Text() | |
} | |
if len(content) >= MaxTwitterLen { | |
iBuffer = append(iBuffer, content[0:MaxTwitterLen]+TailString) | |
restContent = content[MaxTwitterLen:] | |
content = "" | |
counter += 1 | |
} else { | |
restContent = "" | |
} | |
} | |
elapsed := time.Since(start) | |
fmt.Println(iBuffer[0]) | |
fmt.Println(iBuffer[1]) | |
fmt.Println(iBuffer[2]) | |
fmt.Println(len(iBuffer)) | |
fmt.Println(elapsed) | |
if err := scanner.Err(); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment