title: How to marshal an embedded struct in go?
body: <p>I've come across a case which is new to me in go. I think it is something similar to this question <a href="https://stackoverflow.com/questions/38489776/idiomatic-way-to-embed-struct-with-custom-marshaljson-method">Idiomatic way to embed struct with custom MarshalJSON() method</a></p>
  <p>Here is an example of code that I am trying to apply</p>
  <p><strong>main.go</strong></p>
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      &quot;context&quot;
      &quot;fmt&quot;
      &quot;os&quot;
  
      &quot;github.com/ethereum/go-ethereum/ethclient&quot;
  )
  
  func main() {
  
      icpPath, _ := os.LookupEnv(&quot;GETH_ICP_PATH&quot;)
      
      client, _ := ethclient.Dial(icpPath)
      ctx := context.Background()
  
      header, _ := client.HeaderByNumber(ctx, nil)
      data, _ := header.MarshalJSON()
  
      fmt.Println(string(data))
  
  }
  </code></pre>
  <p><strong>output</strong></p>
  <pre class="lang-json prettyprint-override"><code>{&quot;parentHash&quot;:&quot;0x0000000000000000000000000000000000000000000000000000000000000000&quot;,&quot;sha3Uncles&quot;:&quot;0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347&quot;,&quot;miner&quot;:&quot;0x0000000000000000000000000000000000000000&quot;,&quot;stateRoot&quot;:&quot;0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544&quot;,&quot;transactionsRoot&quot;:&quot;0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421&quot;,&quot;receiptsRoot&quot;:&quot;0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421&quot;,&quot;logsBloom&quot;:&quot;0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000&quot;,&quot;difficulty&quot;:&quot;0x400000000&quot;,&quot;number&quot;:&quot;0x0&quot;,&quot;gasLimit&quot;:&quot;0x1388&quot;,&quot;gasUsed&quot;:&quot;0x0&quot;,&quot;timestamp&quot;:&quot;0x0&quot;,&quot;extraData&quot;:&quot;0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa&quot;,&quot;mixHash&quot;:&quot;0x0000000000000000000000000000000000000000000000000000000000000000&quot;,&quot;nonce&quot;:&quot;0x0000000000000042&quot;,&quot;baseFeePerGas&quot;:null,&quot;hash&quot;:&quot;0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3&quot;}
  </code></pre>
  <p>In this output for example the field <code>&quot;difficulty&quot;</code>  has a value of <code>&quot;0x400000000&quot;</code></p>
  <p>I can marshal this field like this:</p>
  <pre class="lang-golang prettyprint-override"><code>difficulty, _ := header.Difficulty.MarshalJSON()
  fmt.Println(string(difficulty))
  </code></pre>
  <p>and get the following output</p>
  <pre><code>17179869184
  </code></pre>
  <hr />
  <p>the type of <code>header</code> is as defined here
  <a href="https://github.com/ethereum/go-ethereum/blob/master/core/types/block.go" rel="nofollow noreferrer">https://github.com/ethereum/go-ethereum/blob/master/core/types/block.go</a></p>
  <pre class="lang-golang prettyprint-override"><code>// Header represents a block header in the Ethereum blockchain.
  type Header struct {
      ParentHash  common.Hash    `json:&quot;parentHash&quot;       gencodec:&quot;required&quot;`
      UncleHash   common.Hash    `json:&quot;sha3Uncles&quot;       gencodec:&quot;required&quot;`
      Coinbase    common.Address `json:&quot;miner&quot;            gencodec:&quot;required&quot;`
      Root        common.Hash    `json:&quot;stateRoot&quot;        gencodec:&quot;required&quot;`
      TxHash      common.Hash    `json:&quot;transactionsRoot&quot; gencodec:&quot;required&quot;`
      ReceiptHash common.Hash    `json:&quot;receiptsRoot&quot;     gencodec:&quot;required&quot;`
      Bloom       Bloom          `json:&quot;logsBloom&quot;        gencodec:&quot;required&quot;`
      Difficulty  *big.Int       `json:&quot;difficulty&quot;       gencodec:&quot;required&quot;`
      Number      *big.Int       `json:&quot;number&quot;           gencodec:&quot;required&quot;`
      GasLimit    uint64         `json:&quot;gasLimit&quot;         gencodec:&quot;required&quot;`
      GasUsed     uint64         `json:&quot;gasUsed&quot;          gencodec:&quot;required&quot;`
      Time        uint64         `json:&quot;timestamp&quot;        gencodec:&quot;required&quot;`
      Extra       []byte         `json:&quot;extraData&quot;        gencodec:&quot;required&quot;`
      MixDigest   common.Hash    `json:&quot;mixHash&quot;`
      Nonce       BlockNonce     `json:&quot;nonce&quot;`
  
      // BaseFee was added by EIP-1559 and is ignored in legacy headers.
      BaseFee *big.Int `json:&quot;baseFeePerGas&quot; rlp:&quot;optional&quot;`
  }
  </code></pre>
  <hr />
  <p>My question is:</p>
  <p>What is the right way to get the json output completly marshaled without going in iterative way through all fields?</p>