Write in file the result?

Hello I have a desire to write in the result of the code file:

package main
import (
“fmt”
“math/big”

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

)

func main() {
// Print header
fmt.Printf("%64s %34s %49s", “Private”, “Public”, “decoded”)

// Initialise big numbers with small numbers
count, one := big.NewInt(0), big.NewInt(10000)

// Create a slice to pad our count to 32 bytes
padded := make([]byte, 32)

// Loop forever because we're never going to hit the end anyway
for {
    // Increment our counter
    count.Add(count, one)

    // 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)
    decoded, version, err := base58.CheckDecode(caddr.EncodeAddress())
        if err != nil {
         fmt.Println(err)
        return
        }
        
    // Print keys
    fmt.Printf("%x %34s %49s", padded, caddr.EncodeAddress(), big.NewInt(0).SetBytes(decoded))
    fmt.Println("", version)
}

}

Just like the previous question you asked here on the forum, please actually specify what you are asking.

It’s not easy to help you when you aren’t actually asking a question.

Please use Google Translate if you aren’t able to properly ask your question in English.

I would like to save to a file the result of the printf

In the example that you have provided, you will loop forever so you can not just write an infinite loop to a file without running out of space.

Here is an example of how you can write a formatted string to a file once you work out what you are actually trying to store:

package main

import (
	"fmt"
	"log"
	"os"
)

func main() {
	// Create a formatted string to write to a file.
	str := fmt.Sprintf("%64s %34s %49s", "Private", "Public", "decoded")

	// Create a new file called file.txt.
	f, err := os.Create("file.txt")
	if err != nil {
		log.Fatalln(err)
	}
	// Close the file when we're done.
	defer f.Close()

	// Write the string str to the file.
	_, err = f.WriteString(str)
	if err != nil {
		log.Fatalln(err)
	}
}

Lastly, based on your 2 questions here so far, I suggest that you go through A Tour of Go and also at least something else like Go by Example so you can learn to understand the basics of Go a little bit better.

If my previous reply is too difficult to understand, here’s an easier helper method to write a string to a file, but I still suggest you trying to learn from the 2 links that I posted above:

package main

import (
	"fmt"
	"io/ioutil"
	"log"
)

func main() {
	// Create a formatted string to write to a file.
	str := fmt.Sprintf("%64s %34s %49s", "Private", "Public", "decoded")

	// Create and write to a new file called file.txt.
	if err := ioutil.WriteFile("file.txt", []byte(str), 0644); err != nil {
		log.Fatalln(err)
	}
}

Unfortunately he wrote only one line

// Write the string str to the file.
_, err = f.WriteString(str)
if err != nil {
	log.Fatalln(err)
}

You can call this as many times as you like in a loop.

i’m test the code :

// Create and write to a new file called file.txt.
for {
_ , err = f.WriteString(str)
}
if err != nil {
log.Fatalln(err)
}

result :

%!(EXTRA uint8=0)1249045137724672133690259881462329489226509877080
%!(EXTRA uint8=0)1249045137724672133690259881462329489226509877080
%!(EXTRA uint8=0)1249045137724672133690259881462329489226509877080
%!(EXTRA uint8=0)1249045137724672133690259881462329489226509877080
%!(EXTRA uint8=0)1249045137724672133690259881462329489226509877080
%!(EXTRA uint8=0)1249045137724672133690259881462329489226509877080

ect…

If you cannot see why your for loop produces this output, I strongly suggest following Benjamin’s advice and learn the basics of the language from a good tutorial or course. It may cost you a little time now but it will pay enormously in the long run.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.