Reading data from text file to struct

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() {
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


Hi @MantasSilanskas welcome to the community!

Have you tried the io/util readFile function?
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 (

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 {
		case 3:
			gps.shift, err = strconv.Atoi(v)
			if err != nil {
		case 4:
			gps.busNumber = v
		case 5:
			gps.lowGrind, err = strconv.Atoi(v)
			if err != nil {
		case 6:
			gps.tripsStart, err = strconv.Atoi(v)
			if err != nil {
		case 7:
			gps.tripsEnd, err = strconv.Atoi(v)
			if err != nil {
		case 8:
			gps.directionID, err = strconv.Atoi(v)
			if err != nil {
		case 9:
			gps.directionType = v

		case 10:
			gps.directionName = v


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.


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


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.


Hello guys,

Thanks for your replies but after long sitting on this code I ended up with parsing all of the data and adding data to structs and maps!

Thanks again for your help and see you soon !


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.