1

Go newbie here.

I am trying to run very simple example on go1.11.4 windows/amd64

Here is my code below;
sandbox: https://play.golang.org/p/GoALi4HYx3L

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    fmt.Print("Enter a grade: ")
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter text: ")
    input := reader.ReadString('\n')
    fmt.Println(input)
}

And I am getting following error:

prog.go:13:28: multiple-value reader.ReadString() in single-value context

Am I missing something here?

Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158
  • It has two return values, and you are assigning them to one variable `input` https://golang.org/pkg/bufio/#Reader.ReadString – nos Dec 23 '18 at 21:53
  • 1
    If you're reading this because of the book "Head First Go," know this example is intended to give that error. You just have to read the paragraphs that follow this code snippet for their explanation on how to fix it. – Nala Nkadi Sep 03 '21 at 18:13

1 Answers1

2

Check the documentation for ReadString, especially the part that describes return values (Tip: it is in the title of the section).

https://golang.org/pkg/bufio/#Reader.ReadString

Also, it worse check this reading also https://gobyexample.com/multiple-return-values

Mikhail
  • 809
  • 8
  • 16
  • 1
    Thanks, that example helped in a good way. Go documentation seems poor, in terms of giving usage examples, compared to Java and Python. – Teoman shipahi Dec 24 '18 at 01:24