各位好
请问这段代码指定行数输出后,自动存档不覆盖前一个已经储存的档案要如何编写?程序执行时,假设每输出 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) } }
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') }
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。