I am trying to capture user input in Go with little luck. I can get non-spaced words to work:
var s string
println("enter string:")
fmt.Scan(&s)
However, the Go documentation says that scan will delimit at spaces and new lines. So I think I have to set up bufio.Reader’s ReadLine. Here is my attempt, which will not compile:
package main
import (
"bufio"
"os"
"fmt"
)
const delim = '\n'
const file = "file"
func main() {
r := bufio.NewReader() *Reader
println("enter string:")
line, err := r.ReadString(delim)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(line)
}
errors:
1.go:14: not enough arguments in call to bufio.NewReader
1.go:14: undefined: Reader
So, how do I define “Reader”? And if it was defined, would this be the correct way to capture the input as a string, delimited at “\n”, and not at the space? Or should I be doing something completely different?
Thanks in advance.
Change
to read
to fix the problem.
The original encantation is incorrect because you seem to just copied and pasted the method’s signature from the spec, but the spec defines the signature, not an example of a call, so
*Readerin there is the method’s return type (the type your variablerwill have). And the method’s sole argument is defined to berd io.Reader; that interface is conveniently implemented by theos.Stdinsymbol which seems like a perfect match for your task.P.S.
Consider reading all the docs in the “Learning Go” documentation section, especially “Effective Go”.