Created
November 1, 2018 20:31
-
-
Save akolybelnikov/61b5eab6e34cfa23bc7d28d235bbe2de to your computer and use it in GitHub Desktop.
Slice challenge in Go
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" | |
"fmt" | |
"os" | |
"sort" | |
"strconv" | |
) | |
func main() { | |
fmt.Println("Enter an integer or press 'x' button to exit this program: ") | |
consoleReader := bufio.NewScanner(os.Stdin) | |
sli := make([]int, 0) | |
for consoleReader.Scan() { | |
input := consoleReader.Text() | |
if input != "x" { | |
if i, err := strconv.Atoi(input); err != nil { | |
fmt.Println("Not accepted input") | |
continue | |
} else { | |
sli = append(sli, i) | |
sort.Ints(sli) | |
fmt.Println(sli) | |
} | |
} else { | |
fmt.Println("Exiting...") | |
os.Exit(0) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment