Here is some sample code:
//In this main function, I am parsing the ascii-datafile and write float64 to a dat2bin.gob file. I have left out the float conversion etc..
func main() {
r, _ := regexp.Compile(";")
file, _ := os.Open("/path/to/datafile/household_power_consumption.txt")
scanner := bufio.NewScanner(file)
var basetime time.Time
var bulk [][]float64
var pkgCounter int
f, err := os.Create("/path/to/datafile/prepareData/dat2bin.gob")
defer f.Close()
gobEncoder = gob.NewEncoder(f)
pkgCounter = 0
for j := 0; scanner.Scan(); j++ {
if j%10000 == 0 && j != 0 {
pkgCounter++
err = gobEncoder.Encode(bulk)
bulk = nil
}
res := r.Split(scanner.Text(), -1)
bulk = append(bulk, res)
}
pkgCounter++
err = gobEncoder.Encode(bulk)
bulk = nil
}
// In this second main function, I am trying to pull out the data from dat2bin.gob packagewise. It is just experimental.
// I first read into stuffList then into stuffList1, and then again convert to a csv-ascii format to check my data
// in human readable format. And here is the problem, these two calls:
// err = gobDecoder.Decode(&stuffList)
// err = gobDecoder.Decode(&stuffList1)
// give me the exact same data 'package'.
func main() {
gobFile, err := os.Open("/path/to/datafile/prepareData/dat2bin.gob")
defer gobFile.Close()
gobDecoder := gob.NewDecoder(gobFile)
var stuffList, stuffList1 [][]float64
err = gobDecoder.Decode(&stuffList)
err = gobDecoder.Decode(&stuffList1)
write1, err := os.Create("stuffList.txt")
write2, err := os.Create("stuffList1.txt")
enc1 := csv.NewWriter(write1)
enc2 := csv.NewWriter(write2)
var strWrite [][]string = make([][]string, 0, 10000)
var strWrite1 [][]string = make([][]string, 0, 10000)
var rows, rows1 int
for i, oneline := range stuffList {
strWrite = append(strWrite, make([]string, 9))
rows++
for j, onecell := range oneline {
strWrite[i][j] = strconv.FormatFloat(onecell, 'f', 6, 64)
}
}
for i, oneline := range stuffList1 {
strWrite1 = append(strWrite, make([]string, 9))
rows1++
for j, onecell := range oneline {
strWrite1[i][j] = strconv.FormatFloat(onecell, 'f', 6, 64)
}
}
fmt.Println(rows)
fmt.Println(rows1)
for i, _ := range strWrite {
enc1.Write(strWrite[i])
enc2.Write(strWrite1[i])
}
enc1.Flush()
enc2.Flush()
}