Golang bytes.CutSuffix() function returns s without the provided leading prefix byte slice and reports whether it found the prefix. If s doesn’t start with a prefix, CutPrefix() returns s, false. If the prefix is the empty byte slice, CutPrefix() returns s, true.
Syntax
func CutSuffix(s, suffix []byte) (before []byte, found bool)
Parameters
- s: It is a byte slice.
- prefix: It is a byte slice.
Return value
It returns s without the provided ending suffix byte slice and reports whether it found the suffix. If s doesn’t end with a suffix, it returns s, false. If the suffix is the empty byte slice, it returns s, true.
Example 1
package main
import (
"bytes"
"fmt"
)
func main() {
s := []byte("Hello World!")
suffix := []byte("World!")
before, found := bytes.CutSuffix(s, suffix)
fmt.Printf("%s %t\n", before, found)
}
Output
Hello true
Example 2
package main
import (
"bytes"
"fmt"
)
func main() {
s := []byte("Hello World!")
suffix := []byte("World")
before, found := bytes.CutSuffix(s, suffix)
fmt.Printf("%s %t\n", before, found)
}
Output
Hello World! false
Example 3
package main
import (
"bytes"
"fmt"
)
func main() {
s := []byte("Hello World!")
suffix := []byte("")
before, found := bytes.CutSuffix(s, suffix)
fmt.Printf("%s %t\n", before, found)
}
Output
Hello World! true
That’s it!
Related posts

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.