read_file2.go 528 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // read_csvfile.go
  2. package main
  3. import (
  4. "fmt"
  5. "os"
  6. // "io/ioutil"
  7. // "strings"
  8. )
  9. func main() {
  10. file, err := os.Open("products2.txt")
  11. if err != nil {
  12. panic(err)
  13. }
  14. defer file.Close()
  15. var col1, col2, col3 []string
  16. for {
  17. var v1, v2, v3 string
  18. _, err := fmt.Fscanln(file, &v1, &v2, &v3)
  19. if err != nil {
  20. break
  21. }
  22. col1 = append(col1, v1)
  23. col2 = append(col2, v2)
  24. col3 = append(col3, v3)
  25. }
  26. fmt.Println(col1)
  27. fmt.Println(col2)
  28. fmt.Println(col3)
  29. }
  30. /* Output:
  31. [ABC FUNC GO]
  32. [40 56 45]
  33. [150 280 356]
  34. */