Golang : Read until certain character to break for loop
Encounter a situation where I need to tell the editor that is time to save the input and exit while working on the previous tutorial to show how to create a simple text based editor to accept screen input and save the content directly into a file.
Basically, I need to tell the program when to stop scanning or reading from the input source and break the for loop.
To do that... just intercept input from scanner.Text() and if it matches the criteria, break the for loop.
Code fragment from https://www.socketloop.com/tutorials/golang-text-file-editor-accept-input-from-screen-and-save-to-file
var userInput []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
if line == "." {
break // to stop writing enter . (period) and press enter
}
userInput = append(userInput, line+"\n")
}
See also : Golang : Text file editor (accept input from screen and save to file)
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16.6k Golang : Get own process identifier
+16.4k Golang : Gzip file example
+18.6k Golang : Padding data for encryption and un-padding data for decryption
+14.2k Golang : Send email with attachment(RFC2822) using Gmail API example
+19.9k Golang : Count number of digits from given integer value
+11.6k Golang : Convert(cast) bigint to string
+6.1k Golang : Detect face in uploaded photo like GPlus
+29k Golang : Record voice(audio) from microphone to .WAV file
+5.8k nginx : force all pages to be SSL
+22.5k Golang : untar or extract tar ball archive example
+16.3k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+4.6k Javascript : How to get width and height of a div?