<p>I'm hoping you guys can help... My expertise is MATLAB, so golang is very new to me and i'm struggling with String Arrays in loops. I am trying to automate Ripple wallet generation for a project, but i'm struggling with the language differences.</p>
<p>I wrote the following code:</p>
<pre><code>package main
import (
"flag"
"fmt"
"log"
"os"
"encoding/csv"
"bitbucket.org/dchapes/ripple/crypto/rkey"
)
func main() {
for i:= 0; i < 2000; i++ {
secret := flag.String("secret", "",
"Ripple secret, if empty generate a random one")
flag.Parse()
var s *rkey.FamilySeed
var err error
if *secret != "" {
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 == "" {
if b, err := s.MarshalText(); err != nil {
log.Fatal(err)
} else {
*secret = string(b)
}
fmt.Println(" secret:", *secret)
fmt.Println("address:", addr)
} else {
fmt.Println(addr)
}
var data[i]=[]string{*secret, addr}
}
CSVExport(data)
}
func CSVExport(data []string){
file, err := os.Create("result.csv")
checkError("Cannot create file", err)
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
for _, value := range data {
err := writer.Write(value)
checkError("Cannot write to file", err)
}
}
func checkError(message string, err error) {
if err != nil {
log.Fatal(message, err)
}
}
</code></pre>
<p>I don't understand why this isn't working - can I not store a string array in data[i]? I get the errors: unexpected =, expecting type for the 'var data[i]=' and non-declaration statement outside function body for line 'CSVExport(data)'. I can'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'll also have to address the data you'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 "secret" 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>
Struggling with learning Golang - trying to export a series of cryptocurrency addresses to a csv file
blov · · 478 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传