# How to Create a Bitcoin WIF in GoLang

{% embed url="<https://youtu.be/6cHmEEIxMX0>" %}

A **WIF (Wallet Import Format)** key is a Base58Check-encoded version of a **private key**. It allows private keys to be **easily imported and exported** across wallets while including a **checksum** to catch errors.

***

#### Example Implementation in GoLang

```
package main 

import ( 
    "fmt" 
    "github.com/libsv/go-bk/base58" 
    "github.com/libsv/go-bk/bec" 
    "github.com/libsv/go-bk/crypto" 
)   

func main() { 
    // 1. Generate a new private key
    privKey, _ := bec.NewPrivateKey(bec.S256()) 

    // 2. Serialize the private key
    pKey := privKey.Serialise() 

    // 3. Add version byte (0x80 for mainnet, 0xef for testnet)
    version := []byte{0x80}  
    pKey = append(version, pKey...) 

    // 4. Append compression flag (0x01 means compressed public key will be used)
    pKey = append(pKey, 0x01) 

    // 5. Double SHA-256 and take first 4 bytes (checksum)
    chksum := crypto.Sha256d(pKey)[:4] 

    // 6. Append checksum
    pKey = append(pKey, chksum...) 

    // 7. Encode in Base58Check
    w := base58.Encode(pKey)   

    // 8. Print result
    fmt.Printf("WIF: %s\n", w)   
}
```

#### Step-by-Step Process

1. **Serialize the private key (`d`)**

   &#x20;`pKey := privKey.Serialise()`&#x20;
2. **Add version byte**

   * `0x80` for **mainnet**
   * `0xef` for **testnet**

   &#x20;`version := []byte{0x80} pKey = append(version, pKey...)`&#x20;
3. **Append compression byte (0x01)**

   * Marks that the **corresponding public key** is compressed.

   &#x20;`pKey = append(pKey, 0x01)`&#x20;
4. **Generate checksum** (first 4 bytes of double SHA-256)

   &#x20;`chksum := crypto.Sha256d(pKey)[:4]`
5. **Append checksum**

   &#x20;`pKey = append(pKey, chksum...)`&#x20;
6. **Encode with Base58**

   &#x20;`w := base58.Encode(pKey)`&#x20;
7. **Print the final WIF**

   &#x20;`fmt.Printf("WIF: %s\n", w)`

***

✅ **Summary:**\
A **WIF key** is a Base58Check-encoded private key with a **version byte**, **compression flag**, and **checksum**. This format makes private keys portable, error-resistant, and widely supported across Bitcoin wallets.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hub.bsvblockchain.org/higher-learning/bsv-academy/bitcoin-primitives-hash-functions/ripemd-160-overview/how-to-create-a-bitcoin-wif-in-golang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
