Git Product home page Git Product logo

csv's Introduction

csv

golang structs to csv file

Install

go get github.com/sundy-li/csv

Tag to title mapping

You could map the struct to csv indicating the title by using this

type People struct {
    Name   string `csv:"姓名"`
    Age    int
    Sex    string
    Weight float32 `csv:"体重"`
    Marry  bool    `csv:"婚姻状况"`
}
  • ** If you have not indicated the csv tag , it will just use the field name as title **

Write to file

func main() {

    var peoples = make([]*People, 0, 10)
    for i := 0; i < 10; i++ {
        var people = &People{
            Name:   "someone",
            Age:    22,
            Sex:    "美女",
            Weight: 90.6,
            Marry:  false,
        }
        peoples = append(peoples, people)
    }

    file, _ := os.OpenFile("aa.csv", os.O_CREATE|os.O_RDWR, 0644)
    
    //if only file implements the io.Writer interface 
    var parser = csv.NewCsv(file)
    err := parser.Parse(peoples)
    if err != nil {
        println(err.Error())
    }
    file.Close()
}

Run this code and it will build an csv file

Write to the ResponseWriter

func main() {
    http.HandleFunc("/", saveCsv)
    http.ListenAndServe(":8080", nil)
}

func saveCsv(w http.ResponseWriter, req *http.Request) {
    var randFile = time.Now().String() + ".csv"
    w.Header().Set("Content-Type", "application/octet-stream;charset=utf-8")
    w.Header().Set("Content-Disposition", "attachment;filename="+randFile)
    var peoples = make([]*People, 0, 10)
    for i := 0; i < 10; i++ {
        var people = &People{
            Name:   "someone",
            Age:    22,
            Sex:    "美女",
            Weight: 90.6,
            Marry:  false,
        }
        peoples = append(peoples, people)
    }
     //if only ResponseWriter implements the io.Writer interface 
    var parser = csv.NewCsv(w)
    err := parser.Parse(peoples)
    if err != nil {
        println(err.Error())
    }
    return
}
  • ** Run this code,while you visit 127.0.0.1:8080/ you will download the csv file **

csv's People

Contributors

sundy-li avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.