Why is it so hard to just take user input!

I just started learning golang with a good state of mind, but even though it is termed simplistic language, there are so many ways just to take the user input and all of them are so confusing.

I may just be hanging on with my views from other languages which I was told not to before getting into golang, but still, how come in a modern language one has to type like 4-5 lines to take user input.

Kindly help me by checking my code, and also can it be optimized? It is to input an array using bufio. Kindly pardon me since it’s been just 2 days since I began learning.


// input function
func getInput(prompt string, r *bufio.Reader) (string, error) {
	fmt.Println(prompt)
	input, err := r.ReadString('\n')
	if err != nil {
		log.Fatal("Error while reading input!")
	}
	return strings.TrimSpace(input), err
}

// main function
func main() {
	reader := bufio.NewReader(os.Stdin)              // Reader
	input, _ := getInput("Enter the size: ", reader) // Get Input function
	size, _ := strconv.Atoi(input)                   // Parse string to input

	var arr []int // decclare a slice
	for i := 0; i < size; i++ {
		var x int
		fmt.Scanf("%d", &x)
		arr = append(arr, x)
	}
	fmt.Println(arr)
}

I do not know if this apply to your case, but If you use a web form you can use Javascript to get the input from the form.

var myForm = document.getElementById('form');
myForm.addEventListener('submit', function(event) {
  var formData = new FormData(myForm)
  var object = {};
  formData.forEach(function(value, key) {
    object[key] = value;
  });
  var json = JSON.stringify(object)
  alert(json)
});

The output is JSON that you can process both in Go and Javascript.

how come in a modern language one has to type like 4-5 lines to take user input.

I’m assuming you’re talking about getInput. For the sake of brevity, and ignoring errors (like many languages idiomatically do), you could read input from stdin like so:

var value string
fmt.Scanln(&value)

But let’s take a look at what you’re doing in getInput with some comments:

func getInput(prompt string, r *bufio.Reader) (string, error) {
	// Print the prompt to the user (which has nothing to do with reading input)
	fmt.Println(prompt)
	// Read the input
	input, err := r.ReadString('\n')
	// If there was an error, deal with it.
	if err != nil {
		log.Fatal("Error while reading input!")
	}
	// Trim input and return it
	return strings.TrimSpace(input), err
}

You’re doing more than one thing; each thing more or less has its’ own line of code. What’s not to like about that? Or rather, could you explain what part of it you find confusing or not simple? Because this seems simple to me.

That said, I think the term “simple” is a bit complicated in this case. Writing software is almost never simple. In the case of go, I think people use that term because go is opinionated and doesn’t have a lot of language bloat, which encourages simple libraries/frameworks and lightning fast compile times. But it’s not simple in the sense that it uses magic to abstract details away from you. You must know what you are doing and be intentional about it. You also have to deal with errors or intentionally not deal with them.

4 Likes

I understand now. I really think i misunderstood what simple meant in terms of Golang. I thought of it as a language with short and straightforward syntax.

But yeah your post makes a lot of sense :slight_smile: