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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题