Upgrade to Pro — share decks privately, control downloads, hide ads and more …

encoding/csv

pinzolo
April 29, 2017

 encoding/csv

そうだ Go、京都。でのLTスライド

pinzolo

April 29, 2017
Tweet

More Decks by pinzolo

Other Decks in Technology

Transcript

  1. $ whoami » NODA Masato » @pinzolo » גࣜձۭࣾ »

    Goྺ1೥ະຬ » goroutine ࢖͍͜ͳ͍ͤͯͳ͍੎ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)
  2. csvutil CSVʹରͯ͋͠Ε͜ΕͰ͖ΔϠπ $ csvutil generate --size 5 --count 10 --header

    ࢯ໊:༣ศ൪߸:ॅॴ:ݐ෺:ϝʔϧ | \ csvutil name --name ࢯ໊ | \ csvutil address --zip-code ༣ศ൪߸ --prefecture ॅॴ --city ॅॴ --town ॅॴ --block-number | \ csvutil building --column ݐ෺ | \ csvutil email --column ϝʔϧ ࢯ໊,༣ศ൪߸,ॅॴ,ݐ෺,ϝʔϧ ౔ా ܚଠ࿠,061-8035,ژ౎෎࿨ޫࢢ੢य़ผӺલ੢ொ42-18,ϓϨεςʔδલՏݪ1926,[email protected] ݹ઒ ಞਓ,468-2410,௕࡚ݝ౦ࠜࢢޚଂౡଜҰԁ16,ςϥεϥΠϑ1421,[email protected] ԣా ඣѮ,197-3356,Ѫ஌ݝࢤ໦ࢢਆాࠤٱؒՏ؛44-12-6,ιϨΠϢຑੜ1229,[email protected] Ԯ໺ ٢จ,819-3023,౦ژ౎େࡕࢢฏ໺ُ۠ඌொ44-24-2,άϦʔϯΤεςʔτ1727,[email protected] ৿ࢁ ࠫߐ,289-9318,Ԭࢁݝେࡕࢢ౦ཎ઒۠ࡩ࡞42-6,άϨʔεϰΟϨοδӉ࿨ொ໩ॴ1118,[email protected] େ໺ ࠫف,093-0401,৽ׁݝՏ๺܊಺ṗொ໦ڵொ46-10,ύʔϧϨδσϯεསέݪ806,[email protected] ࢁ࡚ ྰࡊ,065-9621,ฌݿݝେࡕࢢ࿘଎۠ീ഼ொ39-18,εΠʔτίʔτޫொ506,[email protected] ۚ୔ ༏ೋ,201-6394,ಙౡݝؠख܊࣢ੴொ೔ஔ໺ా36-10,ϓϨεςʔδീ৿Ṥ௕421,[email protected] দݪ ੣ޗ,877-1329,ੴ઒ݝ٢໺܊໺ഭ઒ଜṗ38-5-10,Ψʔσϯλϫʔ౻Տ಺1120,[email protected] ࡔҪ ྗ໵,967-7366,Ѫඤݝେࡕࢢ੢۠നௗொத௡԰41,ϨΠΫύʔΫ609,[email protected] ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)
  3. ओʹ࢖͏΍ͭ csv.Reader io.Reader Λ౉ͯ͠ Read ͔ ReadAll ͢Δ͚ͩɻ csv.Writer io.Writer

    Λ౉ͯ͠ Write -> Flush ͔ WriteAll ͢Δ͚ͩɻ ͲͪΒ΋ io.Reader io.Writer ͱޓ׵ੑ͸ͳ͘ɺ·ͨ interface Ͱ΋͋ Γ·ͤΜɻ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)
  4. αϯϓϧίʔυ src, _ := os.Open("src.csv") defer src.Close() dst, _ :=

    os.Create("dst.csv") defer dst.Close() r := csv.NewReader(src) w := csv.NewWriter(dst) for { record, err := r.Read() if err != nil { if err == io.EOF { break } panic(err) } w.Write(record) } ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)
  5. จࣈίʔυ csv.Reader ΋ csv.Writer ΋σϑΥϧτͰ͸จࣈྻΛUTF-8ͱͯ͠ѻ͍ ·͢ɻ ͨͩ͠BOMʹ͸ରԠ͍ͯ͠·ͤΜɻ BOMΛѻ͏ʹ͸Ұ޻෉ඞཁͰ͢ɻ1 ͓͔͛Ͱ CSV

    ͱ͍͑͹ Excel Ͱ͕ܾͯ͢͠૬ੑ͸Α͋͘Γ·ͤΜɻ 1 GoͰBOM෇͖ͷUTF8ͳCSVΛѻ͏ʹ͸ | tail -f pinzo.log ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)
  6. ίϝϯτ csv.Reader ʹ͸ Comment ϓϩύςΟ͕͋Γɺ͜ͷ஋͔Β࢝·ΔߦΛί ϝϯτߦͱͯ͠εΩοϓ͠·͢ɻ ͨͩ͠ rune ͳͷͰ //

    Ͱ࢝·ΔߦΛίϝϯτߦͱΈͳ͢Α͏ͳ͜ͱ͸Ͱ ͖·ͤΜɻ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)