Attempting to exit on key character press with an enter afterwards

No you shouldn’t be disappointed. Smart people asks for help and you managed to push so much further.

1 Like

I agree with Johan. Don’t feel bad about it at all!

Here’s a story for you from “ancient history”: I started learning C in 1981 when there was only one book in the world on the subject, and the the World Wide Web wasn’t even an idea. All I had was The C Programming Language (by Brian Kernighan and Dennis Ritchie, who both worked to develop Unix and C at Bell Labs), and there were parts of it I simply did not understand. I was having trouble with C because my previous experience with programming was with BASIC, Fortran, and a very odd language called APL (from which Go gets the iota constant thingy). I had absolutely no exposure to Algol or anything related to it. I liked C very much, but the ideas were strange to me.

I was really lucky I had a good friend who knew the language well already. I asked him a bunch of stupid questions that I’d be really embarrassed to have printed on the Internet today! I’m sure my dumb newbie questions amused him, and thankfully, he took the time to explain simple things to me. I went on to learn C really well, and up until recently, when I started learning Go, C was my favorite programming language. I’ve used it for all sorts of things for over 35 years and counting.

I hope you and others here on the forum who are just getting started with Go eventually learn Go as well as I learned C! I’m happy to spend some time answering simple questions because I still remember what it was like to need a little help getting started, and how valuable that help was to me.

1 Like

I know this topic is very old, but for a simpler approach to anybody who would visit here, I came up with this solution, keeping in mind about the exit when character is entered and the 0s in the slice. The problem is we cannot compare runes that identify as alias for int32 with the character runes (not a proper term but I hope you get the idea of it). When we enter a when the loop starts, and we enter an integer, and then a character, the rune would not accept that character, more like it skips is what I’ve noticed, so the integer value from the past remains the same. So resetting the value of the input rune at the beginning of every loop might solve this. But there would be a problem not being able to use that integer again in the slice. I’m sorry about that, but here it is, let me know if this helps :slight_smile:

package main

import (
	"fmt"
	"sort"
)

func main() {
	var ele rune
	var size int
	var sli = make([]int,0,3)
	size = cap(sli)
	for i:=0; i<=size; i++{
		if i>=len(sli){
			size=size+1
		}
		ele = 0
		fmt.Println("Enter a number to add: ")
		fmt.Scan(&ele)
		if ele==0 {
			fmt.Println("Stopping!")
			break
		}
		sli = append(sli, int(ele))
	}
	sort.Ints(sli)
	fmt.Println(sli)
}