Created
June 28, 2021 18:34
-
-
Save cnaude/d198a16bb6c60d9fe9baec70a064c69d to your computer and use it in GitHub Desktop.
create random strings based on file input
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" | |
"flag" | |
"fmt" | |
"math/rand" | |
"os" | |
"strconv" | |
"time" | |
) | |
func main() { | |
var a []string | |
inputFile := flag.String("i", "input.txt", "input file") | |
count := flag.String("c", "12", "count") | |
flag.Parse() | |
c, err := strconv.Atoi(*count) | |
if err != nil { | |
fmt.Printf("Error: %s\n", err) | |
os.Exit(1) | |
} | |
file, err := os.Open(*inputFile) | |
if err != nil { | |
fmt.Printf("Error: %s\n", err) | |
os.Exit(1) | |
} | |
defer file.Close() | |
scanner := bufio.NewScanner(file) | |
for scanner.Scan() { | |
a = append(a, scanner.Text()) | |
} | |
rand.Seed(time.Now().UnixNano()) | |
for { | |
for j := 0; j <= c-1; j++ { | |
r := rand.Intn(len(a)) | |
fmt.Printf("%s ", a[r]) | |
} | |
fmt.Println("") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment