dongxi6897 2016-07-31 19:43 采纳率: 0%
浏览 10
已采纳

为什么Golang中stdin生成的数组将最后一项转换为零?

Note: I am new to StackOverflow as well as to Programming, so if my question is not "so professional" or "well formatted", please forgive me.

I am using the following Go (Golang) code to capture some space-separated numbers (string) from terminal, then split it into a slice. Later I'm converting this slice to a slice of float64 by getting one item at a time from the strings-slice and converting it to float64 and appending it to the float64-slice. Then I'm returning the resulting float64 slice and printing it in the main function. The problem is when I pass some space-separated digits to the terminal, the last digit is converted to zero.

for example if I pass 1 2 3 4 5 I expect the resulting slice as [1 2 3 4 5], but it gives me the slice as [1 2 3 4 0].

I'm trying from the last 5 hours, but I'm not able to find what I'm missing or messing.

code:

package main

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

func main() {
    a := ReadInput()
    fmt.Println(a)
}

func ReadInput() []float64 {
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter text: ")
    text, _ := reader.ReadString('
')

    textSlice := strings.Split(text, " ")
    floatsSlice := make([]float64, 0)
    for _, elem := range textSlice {
        i, _ := strconv.ParseFloat(elem, 64)
        floatsSlice = append(floatsSlice, i)
    }

    return floatsSlice
}

Thank You in advance!

  • 写回答

1条回答 默认 最新

  • duanmu5039 2016-07-31 20:10
    关注

    ReadString reads until the first occurrence of delim in the input,
    returning a string containing the data up to and including the delimiter.

    so, strings.Split(text, " ") not splits last character so:
    you may use strings.Fields(text) instead of strings.Split(text, " ")
    and always check for errors:
    like this working sample code:

    package main
    
    import (
        "bufio"
        "fmt"
        "os"
        "strconv"
        "strings"
    )
    
    func main() {
        a := ReadInput()
        fmt.Println(a)
    }
    
    func ReadInput() []float64 {
        reader := bufio.NewReader(os.Stdin)
        fmt.Print("Enter text: ")
        text, err := reader.ReadString('
    ')
        if err != nil {
            fmt.Println(err)
        }
    
        textSlice := strings.Fields(text)
        floatsSlice := make([]float64, 0)
        for _, elem := range textSlice {
            i, err := strconv.ParseFloat(elem, 64)
            if err != nil {
                fmt.Println(err)
            }
            floatsSlice = append(floatsSlice, i)
        }
    
        return floatsSlice
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法