Golang : Characters limiter example
Problem:
You want to limit a string to X number of characters. How to do that?
Solution:
Use io.ReadAtLeast()
function to read from the string but limited to the X number of characters.
For example:
package main
import (
"fmt"
"io"
"strings"
)
// Limits a string to X number of characters.
func char_limiter(s string, limit int) string {
reader := strings.NewReader(s)
// create buffer with specified limit of chraracters
buff := make([]byte, limit)
n, _ := io.ReadAtLeast(reader, buff, limit)
if n != 0 {
//fmt.Printf("\n %s ", buff)
return string(buff) + "..."
} else {
// nothing happens, return original string
return s
}
}
func main() {
str := "ReadAtLeast reads from r into buf until it has read at least min bytes. It returns the number of bytes copied and an error if fewer bytes were read. The error is EOF only if no bytes were read. If an EOF happens after reading fewer than min bytes, ReadAtLeast returns ErrUnexpectedEOF. If min is greater than the length of buf, ReadAtLeast returns ErrShortBuffer. On return, n >= min if and only if err == nil."
fmt.Printf("Original : [%s] \n", str)
// limit to 10 characters
fmt.Println("10 characters : ", char_limiter(str, 10))
// limit to 30 characters
fmt.Println("30 characters : ", char_limiter(str, 30))
// limit to 100 characters
fmt.Println("100 characters : ", char_limiter(str, 100))
}
Output:
Original : [ReadAtLeast reads from r into buf until it has read at least min bytes. It returns the number of bytes copied and an error if fewer bytes were read. The error is EOF only if no bytes were read. If an EOF happens after reading fewer than min bytes, ReadAtLeast returns ErrUnexpectedEOF. If min is greater than the length of buf, ReadAtLeast returns ErrShortBuffer. On return, n >= min if and only if err == nil.]
10 characters : ReadAtLeas...
30 characters : ReadAtLeast reads from r into ...
100 characters : ReadAtLeast reads from r into buf until it has read at least min bytes. It returns the number of byt...
See also : Golang : Word limiter 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
+3.4k Java : Random alphabets, alpha-numeric or numbers only string generator
+6.8k Nginx : How to block user agent ?
+6.4k Golang : How to setup a disk space used monitoring service with Telegram bot
+7.6k Golang : What fmt.Println() can do and println() cannot do
+9.3k Golang : Load ASN1 encoded DSA public key PEM file example
+7.2k Golang : Convert(cast) io.Reader type to string
+11.9k Golang : Validate email address
+4.7k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+18.9k Golang : Fix cannot download, $GOPATH not set error
+21.8k Golang : Repeat a character by multiple of x factor
+18.6k Golang : Populate dropdown with html/template example
+7.8k Golang : Metaprogramming example of wrapping a function