Golang : Chunk split or divide a string into smaller chunk example
Problem:
You need a function in Golang that are similar to PHP's chunk_split()
function that can split a string into smaller chunks. It is commonly used in converting base64 encoded string to match RFC 2045 semantics. Such as line-breaking base64 encoded lines at 76 characters.
Excerpt from https://www.ietf.org/rfc/rfc2045.txt
(5) Encoded lines must not be longer than 76 characters, not counting the trailing CRLF. If longer lines are found in incoming, encoded data, a robust implementation might nevertheless decode the lines, and might report the erroneous encoding to the user.
Solution:
This code example below should do the job on dividing a string into smaller chunks.
package main
import (
"fmt"
)
func chunkSplit(body string, limit int, end string) string {
var charSlice []rune
// push characters to slice
for _, char := range body {
charSlice = append(charSlice, char)
}
var result string = ""
for len(charSlice) >= 1 {
// convert slice/array back to string
// but insert end at specified limit
result = result + string(charSlice[:limit]) + end
// discard the elements that were copied over to result
charSlice = charSlice[limit:]
// change the limit
// to cater for the last few words in
// charSlice
if len(charSlice) < limit {
limit = len(charSlice)
}
}
return result
}
func main() {
before := "this is a long string that needs to be chunked into smaller chunks"
// chunk after 30 characters and append with newline
// for RFC 2045, change limit from 30 to 76
after := chunkSplit(before, 30, "\n")
fmt.Println("Before :\n", before)
fmt.Println("After :\n", after)
}
Output:
Before :
this is a long string that needs to be chunked into smaller chunks
After :
this is a long string that nee
ds to be chunked into smaller
chunks
References:
https://socketloop.com/tutorials/golang-warp-text-string-by-number-of-characters-or-runes-example
See also : Golang : Warp text string by number of characters or runes example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.8k Golang : How to check if a website is served via HTTPS
+5k Javascript : Change page title to get viewer attention
+14.8k nginx: [emerg] unknown directive "ssl"
+10.1k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+9.3k Golang : Load ASN1 encoded DSA public key PEM file example
+35.7k Golang : Validate IP address
+25.6k Mac/Linux and Golang : Fix bind: address already in use error
+8k Golang : Convert word to its plural form example
+6.7k CloudFlare : Another way to get visitor's real IP address
+27.3k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+7.1k Golang : Mapping Iban to Dunging alphabets
+16.4k Golang : How to generate QR codes?