title: How to create a 0x appended hex string from a hex string for each byte of hex characters in golang?
body: <p>Using the following as a starting point I was able to get something working.</p>
  
  <p><a href="https://stackoverflow.com/questions/33633168/how-to-insert-a-character-every-x-characters-in-a-string-in-golang">How to insert a Character every X Characters in a String in Golang?</a></p>
  
  But I want to know if there is an idiomatic way to write golang for what I wan to do, which is the output of the first fmt.Println shown below:
  <pre><code>package main
  
  import (
      "bytes"
      "fmt"
  )
  
  func insertStringInto(s string, interval int, sep string) string {
      var buffer bytes.Buffer
      before := interval - 1
      last := len(s) - 1
      for i, char := range s {
          buffer.WriteRune(char)
          if i%interval == before &amp;&amp; i != last {
              buffer.WriteString(sep)
          }
      }
      return buffer.String()
  }
  
  
  func main() {
      fmt.Println("0x" + insertStringInto("01234567891011121314151617181920", 2, ", 0x"))
  
      fmt.Println(insertStringInto("01234567891011121314151617181920", 2, ", 0x"))
  }
  </code></pre>
  
  <p>Produces the following:</p>
  
  <pre><code>0x01, 0x23, 0x45, 0x67, 0x89, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20
  01, 0x23, 0x45, 0x67, 0x89, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20
  </code></pre>
  
  <p>Link to play.golang.org below:</p>
  
  <p><a href="https://play.golang.org/p/TvLFAKsrcCB" rel="nofollow noreferrer">https://play.golang.org/p/TvLFAKsrcCB</a></p>