开发者社区> 问答> 正文

请教有关于指定行数输出存档不覆盖要如何编写

各位好
请问这段代码指定行数输出后,自动存档不覆盖前一个已经储存的档案要如何编写?程序执行时,假设每输出 230 万行后,自动储存一个档案,取名档案 A, 又输出 230 万行自动储存一个档案,取名档 B 以此类推下去...

package main

import ( "fmt" "math/big" "os"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"

)

func main() { // Initialise big numbers with small numbers count, one := new(big.Int), big.NewInt(1) count.SetString("15194206377246025674054756204990322111942063772460256740547562053972000", 10)

file, _ := os.Create("a.txt")
// Create a slice to pad our count to 32 bytes
padded := make([]byte, 32)
limit := 1 << 4
jops := make(chan *big.Int, 2*limit)
for i := 0; i < limit; i++ {
    go worker(jops, padded, file)
}
// Loop forever because we're never going to hit the end anyway
for {
    // Increment our counter
    count.Add(count, one)
    jops <- count
}

}

func worker(jops <-chan *big.Int, padded []byte, file *os.File) { for count := range jops { // Copy count value's bytes to padded slice copy(padded[32-len(count.Bytes()):], count.Bytes()) // Get public key _, public := btcec.PrivKeyFromBytes(btcec.S256(), padded) // Get compressed and uncompressed addresses caddr, _ := btcutil.NewAddressPubKey(public.SerializeCompressed(), &chaincfg.MainNetParams) uaddr, _ := btcutil.NewAddressPubKey(public.SerializeUncompressed(), &chaincfg.MainNetParams) // Print keys row := fmt.Sprintf("%x\n%34s\n%34s\n", padded, uaddr.EncodeAddress(), caddr.EncodeAddress()) fmt.Print(row) fmt.Fprint(file, row) } }

展开
收起
问问小秘 2020-01-07 14:05:53 405 0
1 条回答
写回答
取消 提交回答
  • package main

    import ( "fmt" "math/big" "os"

    "github.com/btcsuite/btcd/btcec"
    "github.com/btcsuite/btcd/chaincfg"
    "github.com/btcsuite/btcutil"
    

    )

    func main() { // Initialise big numbers with small numbers count, one := new(big.Int), big.NewInt(1) count.SetString("25194206377246025674054756204990322111942063772460256740547562051757470", 10) limit := 1 << 4 jops := make(chan *big.Int, limit<<1) lines := make(chan string, limit<<1) writeFile(lines) for i := 0; i < limit; i++ { go worker(jops, lines) } // Loop forever because we're never going to hit the end anyway for { // Increment our counter count.Add(count, one) jops <- new(big.Int).Set(count) } }

    func worker(jops <-chan *big.Int, lines chan<- string) { for count := range jops { // Create a slice to pad our count to 32 bytes padded := make([]byte, 32) // Copy count value's bytes to padded slice copy(padded[32-len(count.Bytes()):], count.Bytes()) // Get public key _, public := btcec.PrivKeyFromBytes(btcec.S256(), padded) // Get compressed and uncompressed addresses caddr, _ := btcutil.NewAddressPubKey(public.SerializeCompressed(), &chaincfg.MainNetParams) uaddr, _ := btcutil.NewAddressPubKey(public.SerializeUncompressed(), &chaincfg.MainNetParams) // Print keys row := fmt.Sprintf("%x\n%34s\n%34s\n", padded, uaddr.EncodeAddress(), caddr.EncodeAddress()) lines <- row fmt.Print(row) } }

    func writeFile(lines <-chan string) { go func() { var i, fileNum uint var file *os.File for line := range lines { if i == 0 || i == 1000000 { file.Close() file, _ = os.Create(i2a(fileNum) + ".txt") fileNum++ i = 0 } file.Write([]byte(line)) i++ } }() }

    func i2a(n uint) string { if n < 26 { return string(n + 'a') } return i2a(n/26 - 1) + string(n%26+'a') }

    2020-01-07 14:10:20
    赞同 展开评论 打赏
问答分类:
Go
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载