Read csv fields to a struct

blov · · 870 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi,</p> <p>I previously did this with a Postgres connection:</p> <pre><code>var varName []MyStruct for rows.Next() { var mS MyStruct if err := rows.Scan(&amp;mS.Field1, &amp;mS.Field2, &amp;mS.Field3); err != nil { log.Println(err) } varName = append(varName, mS) } </code></pre> <p>Is there a way to do the same to a CSV file?</p> <p>Thanks!</p> <hr/>**评论:**<br/><br/>freedsnout: <pre><p>This is how you do it with CSV: <a href="https://play.golang.org/p/0mrCKQoGIJ" rel="nofollow">https://play.golang.org/p/0mrCKQoGIJ</a></p></pre>Kipzy: <pre><p>Thanks, that worked, but had to make a little workaround to fit my needs :)</p> <p>Maybe you could help me with another problem i&#39;m having.</p> <p>My csv has a timestamps like this </p> <pre><code>2015-12-23 00:00:03+0000 </code></pre> <p>I can&#39;t find a way to format it right so I can use it. </p> <p>What can i do? thanks!</p></pre>freedsnout: <pre><p>Go&#39;s <a href="https://godoc.org/time#Parse" rel="nofollow">time.Parse</a> and <a href="https://godoc.org/time#Format" rel="nofollow">time.Format</a> functions are a little unconventional if you&#39;re used to the way most programming languages deal with time parsing and formatting.</p> <p>You need to take a sample timestamp from the data you&#39;re trying to parse. In this case, <code>2015-12-23 00:00:03+0000</code>, and then you need to change the numbers so that the time (and timezone) is exactly <strong>Mon Jan 2 15:04:05 -0700 MST 2006</strong>. And that looks a bit like this:</p> <p><a href="https://play.golang.org/p/-GdTeA51-s" rel="nofollow">https://play.golang.org/p/-GdTeA51-s</a></p></pre>Kipzy: <pre><p>oh, i see. That will help a lot!</p> <p>Thanks!</p></pre>Kipzy: <pre><p>well, had to turn back to this point again.</p> <p>Some of my fields aren&#39;t strings, is there a way to make it work? besides converting the strings?</p></pre>rv77ax: <pre><p>No.</p> <p>Edit: Yes, change your field to string.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

870 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传