Skip to content

Instantly share code, notes, and snippets.

@akolybelnikov
Created November 1, 2018 20:31
Show Gist options
  • Save akolybelnikov/61b5eab6e34cfa23bc7d28d235bbe2de to your computer and use it in GitHub Desktop.
Save akolybelnikov/61b5eab6e34cfa23bc7d28d235bbe2de to your computer and use it in GitHub Desktop.
Slice challenge in Go
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