Sunday, July 27, 2014

Simple writing/reading file in go ( golang )

So everyone needs to know how to write or read file in different languages or envs. And me too =(
In my case I needed to write and read compressed data into/from files using go - and now I'll just write how to do it.

So if we will look to standard library of gopher - we will see simple variant for writing files:

ioutil.WriteFile(filename string, data []byte, perm os.FileMode)



With complete function :

func WriteData(path string, data string) {


    write_data := []byte(data)
    compressed := compress_data(write_data)
    err := ioutil.WriteFile(path, compressed.Bytes(), 0644)


    if err != nil {
        panic(err)
    }


}



For reading data from file - also nothing difficult

ioutil.ReadFile(filename string)


Function :

func ReadData(path string) string {
    dat,  err := ioutil.ReadFile(path)


    if err != nil {
        panic(err)
    }


    return string(string(dat))

} 



Using these two functions we already can write module which will parse command line arguments and write input data into file or get data from file. But I needed to compress data during writing. Easiest way to do it - just to take compress/zlib library. With compressing data - there is no problem at all:

var write_bytes bytes.Buffer 



writer := zlib.NewWriter(&write_bytes)

writer.Write(data)

writer.Close()


and after these manipulation we can write compressed data into file. With uncompressing data - we could have one small issue - during read compreessed data we should create zlib.NewReader, as a result we will have io.Writer type to manipulate after uncompressing. For dealing with such staff we could just to convert it in bytes.Buffer. Result function will looks like :

func uncompress_data(data []byte) Bytes.Buffer {
    var buffer bytes.Buffer

    buff := []byte(data)

    reader := bytes.NewReader(buff)
    read, err := zlib.NewReader(reader)


    if e != nil {
        panic(e)
    }


    buffer.ReadFrom(read)
    return buffer
}
and all module which can read and write compressed data into files:
 package datadriver

import (
    "bytes"
    "io/ioutil"
    "compress/zlib"
)

func check(e error) {
    if e != nil {
        panic(e)
    }
}



func ReadData(path string) string {
    dat, err := ioutil.ReadFile(path)
    check(err)
    buffer := uncompress_data(dat)
    return string(buffer.String())

}

func WriteData(path string, data string) {
    write_data := []byte(data)
    compressed := compress_data(write_data)
    err := ioutil.WriteFile(path, compressed.Bytes(), 0644)
    check(err)
}


func compress_data(data []byte) bytes.Buffer {
    var write_bytes bytes.Buffer

    writer := zlib.NewWriter(&write_bytes)
    writer.Write(data)
    writer.Close()
    return write_bytes
}

func uncompress_data(data []byte) Bytes.Buffer {
    var buffer bytes.Buffer

    buff := []byte(data)

    reader := bytes.NewReader(buff)
    read, err := zlib.NewReader(reader)
    check(err)
    buffer.ReadFrom(read)
    return buffer
}

2 comments:

  1. а де про магічний вибір розміра буфера який буде писатись?

    ReplyDelete
    Replies
    1. ну то вже буде без заголовку "простий", взагалі можна і треба так - відкритки файл через os.Open записати в нього байт-аррей і закрити файл. Це я троха пізніше напишу

      Delete