Reading data from text file to struct


(Mantas Silanskas) #1

my struct:
type GPSDataRow struct {
vehicletype string
route string
schedule int
shift int
busnumber string
lowgrind int
tripsstart int
tripsend int
directionid int
directiontype string
directionname string
}
Example of data in file : Autobusai,65,02,1,2350,0,383,388,43576,D>C,
there is 11 data all seperated by comma.
How do I fill my struct with data from file? It’s around 5000+ lines in it

I tried doing this way :
for scanner.Scan() {
s := strings.Split(scanner.Text(), “,”)
if !scanner.Scan() {
break
}
var gpsdataraw GPSDataRow
gpsdataraw.vehicletype, gpsdataraw.route, gpsdataraw.schedule, gpsdataraw.shift, gpsdataraw.busnumber, gpsdataraw.lowgrind, gpsdataraw.tripsstart, gpsdataraw.tripsend, gpsdataraw.directionid, gpsdataraw.directiontype, gpsdataraw.directionname = s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8], s[9], s[10]
var gpsdata = append(gpsdata, gpsdataraw)

}

but no result


(Jaron R. Swab) #2

Hi @MantasSilanskas welcome to the community!

Have you tried the io/util readFile function?
https://golang.org/pkg/io/ioutil/#ReadFile
This will add the data to a variable

Since the file has multiple lines so I would try splitting it on \n first.
Then you can split on the ,.

This will give you a 2D matrix that you can loop through to add the data to your structs.

However, given the information above (and not having a multiline file) here is what I came up with to get the data into the struct:

package main

import (
	"fmt"
	"log"
	"strconv"
	"strings"
)

type GPSDataRow struct {
	vehicleType   string
	route         string
	schedule      int
	shift         int
	busNumber     string
	lowGrind      int
	tripsStart    int
	tripsEnd      int
	directionID   int
	directionType string
	directionName string
}

func main() {
	var err error
	gps := new(GPSDataRow)

	data := "Autobusai,65,02,1,2350,0,383,388,43576,D>C"
	dataSlice := strings.Split(data, ",")

	for i, v := range dataSlice {
		switch i {
		case 0:
			gps.vehicleType = v
		case 1:
			gps.route = v
		case 2:
			gps.schedule, err = strconv.Atoi(v)
			if err != nil {
				log.Fatal(err)
			}
		case 3:
			gps.shift, err = strconv.Atoi(v)
			if err != nil {
				log.Fatal(err)
			}
		case 4:
			gps.busNumber = v
		case 5:
			gps.lowGrind, err = strconv.Atoi(v)
			if err != nil {
				log.Fatal(err)
			}
		case 6:
			gps.tripsStart, err = strconv.Atoi(v)
			if err != nil {
				log.Fatal(err)
			}
		case 7:
			gps.tripsEnd, err = strconv.Atoi(v)
			if err != nil {
				log.Fatal(err)
			}
		case 8:
			gps.directionID, err = strconv.Atoi(v)
			if err != nil {
				log.Fatal(err)
			}
		case 9:
			gps.directionType = v

		case 10:
			gps.directionName = v
		}
	}

	fmt.Println(gps.route)
	fmt.Println(gps.busNumber)
	fmt.Println(gps.directionType)
}

Click here to view in the Go Playground.

PS. You will need to find a way to make multiple names for your structs or you will just overwrite the previous one every time you move to the next line.

Hope this helps.


(Yamil Bracho) #3

I think it would be easier if you use the built-in library encoding/csv to do this task.


(Jaron R. Swab) #4

I’ve used that in the past for a coding interview. It’s great to get the data into a matrix.
There is still the challenge of getting unique names for the structs though.

The solution I came up with is to have the data in an array with a length the same as the number of busses in the ‘file’. Not optimal but it’s a start.

https://play.golang.org/p/ASGnVUEPegD