godumper

package module
v0.0.0-...-21dab28 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2017 License: GPL-3.0 Imports: 5 Imported by: 0

README

godumper

Dump array of interfaces to CSV/XSLX files.

Build Status GoDoc

  • Dump array of interfaces
  • Dump slice of interfaces
  • Dump map of interfaces
  • ...
How to use ?
go get github.com/ahmdrz/godumper

First of all...

package main

import (
	"fmt"

	"github.com/ahmdrz/godumper"
)

type Message struct {
	Id       int `dump:"index"`
	UserFrom int
	UserTo   int
	Text     string
	Time     int64
	IsReaded bool
}

You can use dump:"index" for change header name

If you want to dump a slice

var slice = []Message{
	Message{
		Id:       810625,
		Time:     1475430311,
		Text:     "Hi dude, Are you okay ?",
		UserFrom: 812311,
		UserTo:   722311,
		IsReaded: true,
	},
	Message{
		Id:       187236,
		UserFrom: 722311,
		UserTo:   812311,
		Text:     "Hey buddy,I'm fine",
		Time:     1475430322,
		IsReaded: false,
	},
	Message{
		Id:       715623,
		UserFrom: 722311,
		UserTo:   812311,
		Text:     "😆😆😆 😛",
		Time:     1475430621,
		IsReaded: false,
	},
}

func main() {
	dumper, err := godumper.New(Message{}, godumper.CSV)
	if err != nil {
		panic(err)
	}
	
	err = dumper.Dump(slice)
	if err != nil {
		panic(err)
	}
	
	err = dumper.Save("result.csv")
	if err != nil {
		panic(err)
	}
	fmt.Println("The slice dumped to result.csv")
}

Or if you want to dump an array...

var array = [3]Message{
	Message{
		Id:       810625,
		Time:     1475430311,
		Text:     "Hi dude, Are you okay ?",
		UserFrom: 812311,
		UserTo:   722311,
		IsReaded: true,
	},
	Message{
		Id:       187236,
		UserFrom: 722311,
		UserTo:   812311,
		Text:     "Hey buddy,I'm fine",
		Time:     1475430322,
		IsReaded: false,
	},
	Message{
		Id:       715623,
		UserFrom: 722311,
		UserTo:   812311,
		Text:     "😆😆😆 😛",
		Time:     1475430621,
		IsReaded: false,
	},
}

func main() {
	dumper, err := godumper.New(Message{}, godumper.XSLX)
	if err != nil {
		panic(err)
	}
	
	err = dumper.Dump(array)
	if err != nil {
		panic(err)
	}
	
	err = dumper.Save("result.xslx")
	if err != nil {
		panic(err)
	}
	fmt.Println("The array dumped to result.xslx")
}
DumpAndSave

If you use Save function , another [][]string will created and memory used by not important datas.

So you can use DumpAndSave for save memory , see below :

func main() {
	dumper, err := godumper.New(Message{}, godumper.CSV)
	if err != nil {
		panic(err)
	}
	err = dumper.DumpAndSave(array, "result.csv")
	if err != nil {
		panic(err)
	}
	fmt.Println("The array dumped to result.csv")
}
License

Read the license file

Contribe

Just fork and make pull request. I will happy if you tell me bugs and help me to improve it.

Documentation

Overview

This library can dump slice , array and etc to CSV files.

Index

Constants

View Source
const (
	XSLX int = 10 << iota
	CSV
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Dumper

type Dumper struct {
	Header []string
	Type   interface{}
	Body   [][]string
	// contains filtered or unexported fields
}

Dumper , This struct contains Header , Type and Body. Header is header of CSV file. Body is 2D array for the body of CSV file. And Type is the type of interface which you want to dump.

func New

func New(item interface{}, outputType int) (*Dumper, error)

New , This method return an error only if

Invalid input, input must be a struct

And return the Dumper struct as base of this library. The input must be a struct.

Todo : allow method to receive map values.

func (*Dumper) Dump

func (dumper *Dumper) Dump(inputSet interface{}) error

Dump This method return error if :

	index out of range (body must be a child of header)
 not struct input for body
 input was not a slice or array

And after called , the body is ready for save or something else...

func (*Dumper) DumpAndSave

func (dumper *Dumper) DumpAndSave(inputSet interface{}, output string) error

DumpAndSave This method return error if :

	index out of range (body must be a child of header)
 not struct input for body
 input was not a slice or array

And after called , the body is ready for save or something else...

func (*Dumper) Save

func (dumper *Dumper) Save(filename string) error

Save This method return error if :

	can't create file.
 can't write header.
 can't write body

And after called , Header and Body will available in file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL