从Go中解析文件中的矩阵

I'm having a file with the following contents:

 1  2  3  4  5
 6  0  0  0  7
 8  0  0  0  9
10  0  0  0 11
12 13 14 15 16

What I want is an multidimensional array (or slice) of [][]int. I tried to fiddle around with the scanner.Scanner library:

scan.Init(f) // f is a file
scan.Whitespace = 1<<'\t' | 1<<'' | 1<<' '
tok := scan.Scan()
for tok != scanner.EOF {
    // do something with tok
    if tok == scanner.String {
        fmt.Print("
")
    } else if tok == scanner.Int {
        // Handle int value
        // How do I get the matched token value?
    }       

    tok = scan.Scan()
}

I can't find a way to get the value of the token that was matched.

So two questions:

  1. How do I get the value of any scanned token?
  2. How can I dynamically create that two-dimensional slice/array before knowing it's exact size? (can be any size really)

I wouldn't use the scanner package (which is suited to parse context free grammars, similar to the Go language) for such a simple task of reading numbers from a file. I would either use:

How can I dynamically create that two-dimensional slice/array before knowing it's exact size? (can be any size really)

You can't. Either read the file twice to know the exact size on your 2nd pass, or just use append to dynamically resize the underlying array of the slice. Calling append() multiple times also results in an amortized O(n) behavior, so that shouldn't be a problem.

Ad 1: Scanner.TokenText

Ad 2.: Briefly - if the dims are to be inferred from the text representation, inner cycle: Append to a line slice (say []int) until line break -> num of mx cols. Outer cycle: Append those line slices (to a [][]int) until EOF -> num of mx rows.