Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use fmt.Sscan to parse integers into an array?

Tags:

go

I'm trying to scan a list of integers from a string into an array (or alternatively, a slice)

package main

import "fmt"

func main() {
    var nums [5]int
    n, _ := fmt.Sscan("1 2 3 4 5", &nums)  // doesn't work
    fmt.Println(nums)
}

What do I need to pass as second argument to Sscan in order for this to work?

I know I could pass nums[0], nums[1] ... etc., but I'd prefer a single argument.

like image 937
Johannes Charra Avatar asked Jan 19 '26 16:01

Johannes Charra


2 Answers

I don't think this is possible as a convenient one-liner. As Sscan takes ...interface{}, you would need to pass slice of interfaces as well, hence converting your array first:

func main() {
    var nums [5]int

    // Convert to interfaces
    xnums := make([]interface{}, len(nums))
    for n := range nums {
        xnums[n] = &nums[n]
    }

    n, err := fmt.Sscan("1 2 3 4 5", xnums...)
    if err != nil {
        fmt.Printf("field %d: %s\n", n+1, err)
    }

    fmt.Println(nums)
}

http://play.golang.org/p/1X28J7JJwl

Obviously you could mix different types in your interface array, so it would make the scanning of more complex string easier. For simply space-limited integers, you might be better using strings.Split or bufio.Scanner along with strconv.Atoi.

like image 111
tomasz Avatar answered Jan 21 '26 08:01

tomasz


To allow this to work on more than just hard-coded strings, it's probably better to use a bufio.Scanner, and an io.Reader interface to do this:

package main

import (
    "bufio"
    "fmt"
    "io"
    "strconv"
    "strings"
)

func scanInts(r io.Reader) ([]int, error) {
    s := bufio.NewScanner(r)
    s.Split(bufio.ScanWords)
    var ints []int
    for s.Scan() {
        i, err := strconv.Atoi(s.Text())
        if err != nil {
            return ints, err
        }
        ints = append(ints, i)
    }
    return ints, s.Err()
}

func main() {
    input := "1 2 3 4 5"
    ints, err := scanInts(strings.NewReader(input))
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(ints)
}

Produces:

[1 2 3 4 5]

Playground

like image 43
Intermernet Avatar answered Jan 21 '26 08:01

Intermernet