Golang, is there a better way read a file of integers into an array?

前端 未结 3 1161
無奈伤痛
無奈伤痛 2021-02-04 05:01

I need to read a file of integers into an array. I have it working with this:

package main

import (
    \"fmt\"
    \"io\"
    \"os\"
)

func readFile(filePath         


        
3条回答
  •  迷失自我
    2021-02-04 05:41

    Your solution with fmt.Fscanf is fine. There are certainly a number of other ways to do though, depending on your situation. Mostafa's technique is one I use a lot (although I might allocate the result all at once with make. oops! scratch that. He did.) but for ultimate control you should learn bufio.ReadLine. See go readline -> string for some example code.

提交回复
热议问题