Published Sep 23, 2023
Contribute to Docs

The CutSuffix() function in Go is used to return a specified string without a given suffix. The function returns the specified string without the given suffix and a boolean value confirming the presence of the suffix. It returns true if the given string has the specified suffix, otherwise it returns the full string and false. If the given suffix is an empty string, the function returns the full string and true.


strings.CutSuffix(str, suffix)

Where str is the given string and suffix is the suffix to remove.


The following example demonstrates the use of the strings.CutSuffix() function.

package main
import (
func main() {
str := "Hello Codecademy"
suffix1 := "ademy"
suffix2 := "llo"
suffix3 := ""
fmt.Println(strings.CutSuffix(str, suffix1))
fmt.Println(strings.CutSuffix(str, suffix2))
fmt.Println(strings.CutSuffix(str, suffix3))

The output will be:

Hello Code true
Hello Codecademy false
Hello Codecademy false

All contributors

Looking to contribute?

Learn Go on Codecademy