Golang bytes.CutSuffix() Function

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

  1. s: It is a byte slice.
  2. 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

Golang bytes.CutPrefix()

Golang bytes.Cut()

Golang bytes.ContainsRune()

Golang bytes.ContainsFunc()

Golang bytes.ContainsAny()

Golang bytes.Contains()

Golang bytes.Clone()

Leave a Comment