如何从单行输入中读取多个整数值?

How to read multiple Integer values from a single line of input in go?

我正在开发一个程序,我希望允许用户在出现提示时输入多个整数。

例如:

输入多个整数:1 3 5 7 9 11

我希望它存储在一个切片中

[1 3 5 7 9 11]

我找到了答案

package main

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

func numbers(s string) []int {
    var n []int
    for _, f := range strings.Fields(s) {
        i, err := strconv.Atoi(f)
        if err == nil {
            n = append(n, i)
        }
    }
    return n
}

func GetInputSlice() []int {

    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan() // -------------------------------> was missing this before
    return numbers(scanner.Text())

}

func main() {
    fmt.Println("Enter sequence of Intergers :")
    var fullslice []int
    fullslice = GetInputSlice()
    fmt.Println("Enter sequence of Intergers :", fullslice)

}