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

各位好
请问这段代码指定行数输出后,自动存档不覆盖前一个已经储存的档案要如何编写?程序执行时,假设每输出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)
    }
}
awesee
最佳答案

@omega 你再看一下,应该不会重复了。

4年前 评论
讨论数量: 4
awesee
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')
}
4年前 评论

很感谢您的帮忙,我给您最佳答案 谢谢您

4年前 评论
awesee

@omega 你再看一下,应该不会重复了。

4年前 评论

确定修正过可用没问题 再次感谢您 请收下我的膝盖 大神

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!