Figured out how to add file lines to array

This commit is contained in:
raul 2024-02-06 17:12:29 +01:00
parent 93bd23b848
commit 1bf9e3e27c
1 changed files with 9 additions and 9 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"bufio"
"fmt"
"os"
)
@ -16,18 +17,17 @@ func main() {
filePath := os.Args[1]
names, err := fileReader(filePath)
file, err := os.Open(filePath)
if err != nil {
fmt.Print(err)
}
fmt.Println(names)
}
defer file.Close()
func fileReader(path string) (str string, err error) {
b, err := os.ReadFile(path)
if err != nil {
fmt.Print(err)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
names = append(names, scanner.Text())
}
stre := string(b)
return stre, err
fmt.Println(names)
}