Using the following as a starting point I was able to get something working.
How to insert a Character every X Characters in a String in Golang?
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: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 && i != last {
buffer.WriteString(sep)
}
}
return buffer.String()
}
func main() {
fmt.Println("0x" + insertStringInto("01234567891011121314151617181920", 2, ", 0x"))
fmt.Println(insertStringInto("01234567891011121314151617181920", 2, ", 0x"))
}
Produces the following:
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
Link to play.golang.org below: