First problem I found is that the code ignored the error returned from scan which was “expecting an integer” so I used scanf to scan for a character, but it was also considering the enter press as another character so I added “\n” to the pattern
then to get the digit from the rune you need to subtract from the ‘0’ rune to get the difference between them
here is the code after fixing the issues
package main
import (
"fmt"
"sort"
)
func main() {
slice := make([]int, 0, 3)
var temp rune
for true {
fmt.Println("enter the integers\n")
_, err := fmt.Scanf("%c\n", &temp)
if err!=nil {
fmt.Println(err)
}
if temp == 'x' {
break
}
slice = append(slice, int(temp - '0'))
fmt.Println(slice)
}
sort.Ints(slice)
fmt.Println(slice)
}
An empty slice can’t have a length of 3, as if it has a length of 3, it contains 3 elements.
Please ask your teacher for proper and unambiguous wording in exercises.
That beeing said, whenever you have something that returns an error, then check it. Reading it in case of non-nil might help a lot to come up with a solution.