3

The fmt package has a Scan function that reads stdin, and writes consecutive space separated values to the variables you provide. Is there a way to use this with Slices, so that I could create a slice of sice n, and have scan read n values and put them into the slice?

1
  • You can use bytes.Buffer or bytes.Reader for this purpose. These struct are specifically designed for the purpose you have. Commented Jun 5, 2013 at 9:55

1 Answer 1

2

Just wrap your call to fmt.Scan in a for loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Apparently this can be slow, since fmt.Scan doesn't use buffering. I've seen fmt.Fscan + bufio.Reader(os.Stdin) suggested instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.