Struggling with learning Golang - trying to export a series of cryptocurrency addresses to a csv file

blov · · 478 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m hoping you guys can help... My expertise is MATLAB, so golang is very new to me and i&#39;m struggling with String Arrays in loops. I am trying to automate Ripple wallet generation for a project, but i&#39;m struggling with the language differences.</p> <p>I wrote the following code:</p> <pre><code>package main import ( &#34;flag&#34; &#34;fmt&#34; &#34;log&#34; &#34;os&#34; &#34;encoding/csv&#34; &#34;bitbucket.org/dchapes/ripple/crypto/rkey&#34; ) func main() { for i:= 0; i &lt; 2000; i++ { secret := flag.String(&#34;secret&#34;, &#34;&#34;, &#34;Ripple secret, if empty generate a random one&#34;) flag.Parse() var s *rkey.FamilySeed var err error if *secret != &#34;&#34; { s, err = rkey.NewFamilySeed(*secret) } else { s, err = rkey.GenerateSeed() } if err != nil { log.Fatal(err) } pubkey := s.PrivateGenerator.PublicGenerator.Generate(0) addr := pubkey.Address() if *secret == &#34;&#34; { if b, err := s.MarshalText(); err != nil { log.Fatal(err) } else { *secret = string(b) } fmt.Println(&#34; secret:&#34;, *secret) fmt.Println(&#34;address:&#34;, addr) } else { fmt.Println(addr) } var data[i]=[]string{*secret, addr} } CSVExport(data) } func CSVExport(data []string){ file, err := os.Create(&#34;result.csv&#34;) checkError(&#34;Cannot create file&#34;, err) defer file.Close() writer := csv.NewWriter(file) defer writer.Flush() for _, value := range data { err := writer.Write(value) checkError(&#34;Cannot write to file&#34;, err) } } func checkError(message string, err error) { if err != nil { log.Fatal(message, err) } } </code></pre> <p>I don&#39;t understand why this isn&#39;t working - can I not store a string array in data[i]? I get the errors: unexpected =, expecting type for the &#39;var data[i]=&#39; and non-declaration statement outside function body for line &#39;CSVExport(data)&#39;. I can&#39;t find documentation about this and stack overflow is downvoting me for being a noob. </p> <hr/>**评论:**<br/><br/>gobdgobd: <pre><p>Data is never defined, outside the for loop define it with data := make([]string,0) then append with data = append(data, string)</p> <p>You&#39;ll also have to address the data you&#39;re appending since it must be string. You could make a struct and use and []struct or do sprintf those 2 data points into a string.</p></pre>th3noname: <pre><p>Couple things:</p> <p>Please use gofmt before submitting code. It makes it way easier for everyone to read.</p> <p>You are defining the &#34;secret&#34; flag inside the loop. There is no need to define it 2000 times.</p> <p>In the CSVExport function you are checking for errors, but the function still continous if an error occurs.</p></pre>

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

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