I am writing a program where I take usernames line by line in a .txt file and verify that the username is in it. I have then created a slice and converted the file into a string and appended it to the string slice. I'm now trying to loop over the slice using a for loop to find a username that's in the file and compare it to another string variable that contains the username. I want to check for and see if it is in the slice. What is the best way to do this? I've tried comparing the elements in range using a for loop of the slice with the variable with the username I want to check but it's not working. So in other words I want to find out the best way to take a txt file that contains a list of just usernames added to it from top to bottom, have my program read (loop over) from that text file, and compare it with a predetermined item (username) in a string variable with what's in the text file and determine if it matches or not.
Username (string variable) == Username (in text file)
*Also the username variable will be based on what a user of the program enters into it. So I'm trying to ultimately check if when a user enters their username the program will verify if it's in the file or not. Thanks.
Code example I've tried:
var readSystemCtl []string
readSystemCtl = append(readSystemCtl, string(file))
for _, username := range of readSystemCtl {
if username == input {
//Continue program if true
break
}else {
//Do something else
}
}
Note: string(file) is the text file i'm trying to read from; and input, is the string variable that the user will had input for their username that is predetermined earlier in the program.