> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# SOL 转账

> 使用 litesvm-go 构建并发送 SOL 转账交易

最基础的 Solana 交易：将 SOL 从一个账户转移到另一个账户。在测试更复杂的程序逻辑之前，这是验证 `litesvm-go` 环境配置是否正常的一个很好的初步测试。

## 完整示例

```go
package mytest

import (
    "testing"

    litesvm "github.com/LiteSVM/litesvm-go"
    solana "github.com/gagliardetto/solana-go"
    "github.com/gagliardetto/solana-go/programs/system"
)

func TestSolTransfer(t *testing.T) {
    svm, err := litesvm.New()
    if err != nil {
        t.Fatal(err)
    }
    defer svm.Close()

    // Generate payer + recipient.
    priv, err := solana.NewRandomPrivateKey()
    if err != nil {
        t.Fatal(err)
    }
    payer := priv.PublicKey()
    recipient := solana.NewWallet().PublicKey()

    // Airdrop SOL to the payer.
    if err := svm.Airdrop(payer, 10_000_000_000); err != nil {
        t.Fatal(err)
    }

    // Check initial balance.
    senderInitial, _, err := svm.Balance(payer)
    if err != nil {
        t.Fatal(err)
    }
    t.Logf("sender initial: %.3f SOL", float64(senderInitial)/1e9)

    // Build, sign, and encode the transfer.
    blockhash, err := svm.LatestBlockhash()
    if err != nil {
        t.Fatal(err)
    }
    ix := system.NewTransferInstruction(1_000_000_000, payer, recipient).Build()
    tx, err := solana.NewTransaction(
        []solana.Instruction{ix},
        blockhash,
        solana.TransactionPayer(payer),
    )
    if err != nil {
        t.Fatal(err)
    }
    if _, err := tx.Sign(func(k solana.PublicKey) *solana.PrivateKey {
        if k.Equals(payer) {
            return &priv
        }
        return nil
    }); err != nil {
        t.Fatal(err)
    }
    txBytes, err := tx.MarshalBinary()
    if err != nil {
        t.Fatal(err)
    }

    // Execute the transfer.
    out, err := svm.SendLegacyTransaction(txBytes)
    if err != nil {
        t.Fatal(err)
    }

    if !out.IsOk() {
        t.Fatalf("tx failed: %s\nlogs: %v", out.Error(), out.Logs())
    }

    // Check final balances.
    senderFinal, _, err := svm.Balance(payer)
    if err != nil {
        t.Fatal(err)
    }
    recipientFinal, _, err := svm.Balance(recipient)
    if err != nil {
        t.Fatal(err)
    }
    t.Logf("sender final:    %.6f SOL", float64(senderFinal)/1e9)
    t.Logf("recipient final: %.3f SOL", float64(recipientFinal)/1e9)
    t.Logf("compute units:   %d", out.ComputeUnits())
    t.Logf("fee:             %d lamports", out.Fee())
}
```

## 预期输出

```
sender initial: 10.000 SOL
sender final:    8.999995 SOL
recipient final: 1.000 SOL
compute units:   150
fee:             5000 lamports
```

<Info>
  由于交易手续费（每个签名 5,000 lamport），发送方的最终余额略低于 9 SOL。
</Info>

这是最简单的交易类型。如需测试自定义程序，请参阅[程序测试](/developers/docs/tools/litesvm/go/examples/program-testing)。如需测试与时间相关的逻辑，请参阅[基于时间的测试](/developers/docs/tools/litesvm/go/examples/time-based)。
