title: Looking for Go equivalent of scanf
body: <p>I'm looking for the Go equivalent of scanf().
  I tried with following code:</p>
  
  <pre><code>  1 package main
    2 
    3 import (
    4     "scanner"
    5     "os"
    6     "fmt"
    7 )
    8 
    9 func main() {
   10     var s scanner.Scanner
   11     s.Init(os.Stdin)
   12     s.Mode = scanner.ScanInts
   13     tok := s.Scan()
   14     for tok != scanner.EOF {
   15         fmt.Printf("%d ", tok)
   16         tok = s.Scan()
   17     }
   18     fmt.Println()
   19 }
  </code></pre>
  
  <p>I run it with input from a text with a line of integers.
  But it always output -3 -3 ...</p>
  
  <p>And how to scan a line composed of a string and some integers?
  Changing the mode whenever encounter a new data type?</p>
  
  <p>The Package documentation:</p>
  
  <blockquote>
    <p>Package scanner</p>
    
    <p>A general-purpose scanner for UTF-8
    encoded text.</p>
  </blockquote>
  
  <p>But it seems that the scanner is not for general use.</p>
  
  <p>Updated code:</p>
  
  <pre><code>func main() {
      n := scanf()
      fmt.Println(n)
      fmt.Println(len(n))
  }
  
  func scanf() []int {
      nums := new(vector.IntVector)
      reader := bufio.NewReader(os.Stdin)
      str, err := reader.ReadString('\n')
      for err != os.EOF {
          fields := strings.Fields(str)
          for _, f := range fields {
              i, _ := strconv.Atoi(f)
              nums.Push(i)
          }   
          str, err = reader.ReadString('\n')
      }   
      r := make([]int, nums.Len())
      for i := 0; i &lt; nums.Len(); i++ {
          r[i] = nums.At(i)
      }   
      return r
  }
  </code></pre>
  
  <p>Improved version:</p>
  
  <pre><code>package main
  
  import (
      "bufio"
      "os"
      "io"
      "fmt"
      "strings"
      "strconv"
      "container/vector"
  )
  
  func main() {
      n := fscanf(os.Stdin)
      fmt.Println(len(n), n)
  }
  
  func fscanf(in io.Reader) []int {
      var nums vector.IntVector
      reader := bufio.NewReader(in)
      str, err := reader.ReadString('\n')
      for err != os.EOF {
          fields := strings.Fields(str)
          for _, f := range fields {
              if i, err := strconv.Atoi(f); err == nil {
                  nums.Push(i)
              }   
          }   
          str, err = reader.ReadString('\n')
      }   
      return nums
  }
  </code></pre>