Created
July 29, 2018 18:05
-
-
Save lkrych/4c02818c2e4c6123643ab64a7b675434 to your computer and use it in GitHub Desktop.
helper functions for sortList
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
//helper function for creating input for main func | |
func createFile(numWords int) { | |
if _, err := os.Stat("./generatedStrings.txt"); err != nil { | |
//if file does not exist | |
//create file | |
f, err := os.Create("./generatedStrings.txt") | |
checkErr(err) | |
defer f.Close() | |
for i := 0; i < numWords; i++ { | |
strings := createStringInput() | |
f.Write(strings) | |
} | |
} | |
return | |
} | |
//default helper func for checking errors | |
func checkErr(err error) { | |
if err != nil { | |
log.Fatal("Error: ", err) | |
} | |
} | |
//helper function for creating strings in file | |
func createStringInput() []byte { | |
newWord := generateNewWord(rand.Intn(12)) | |
newWord = append(newWord, '\n') | |
return newWord | |
} | |
const letterBytes = "abcdefghijklmnopqrstuvwxyz" | |
//helper function for generating a random string | |
func generateNewWord(n int) []byte { | |
b := make([]byte, n) | |
for i := range b { | |
b[i] = letterBytes[rand.Intn(len(letterBytes))] | |
} | |
return b | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment