What does a segregated witness transaction look like? I'd like to learn how segregated witness works, and I thought the raw transaction data would be a good place to start

If this is the structure of current transactions:

version | [inputcount] {[txid] [vin] [signature] [sequence]} | [outputcount] {value} {varint} {lockingscript} | [locktime]


What does a segregated witness transaction look like?
From Bitcoin Core's primitive/transaction.h:

/**
 * Basic transaction serialization format:
 * - int32_t nVersion
 * - std::vector&lt;CTxIn&gt; vin
 * - std::vector&lt;CTxOut&gt; vout
 * - uint32_t nLockTime
 *
 * Extended transaction serialization format:
 * - int32_t nVersion
 * - unsigned char dummy = 0x00
 * - unsigned char flags (!= 0)
 * - std::vector&lt;CTxIn&gt; vin
 * - std::vector&lt;CTxOut&gt; vout
 * - if (flags &amp; 1):
 *   - CTxWitness wit;
 * - uint32_t nLockTime
 */


In other words, before the txin count, there is a 0x00 0x01 sequence (which would otherwise be interpreted as a transaction with 0 inputs and 1 output, which cannot be valid), and before the locktime there is a witness record for each input (the txin count is not repeated, it's implicitly assumed to be equal to the txin count given before).