Here, bytes.TrimRight() function is used to trim trailing bytes in slice with given string in go.
TrimRight() built-in function of bytes package returns a subslice by slicing off all trailing UTF-8-encoded code points contained in cutset string.
bytes.TrimRight() function prototype:
func TrimRight(s []byte, cutset string) []byte Input parameters: s: slice of bytes cutset: String to be sliced off from slice of bytes. Return: It returns a subslice of s by slicing off all trailing UTF-8-encoded code points that are contained in cutset string.
Explanation:
1) s := []byte("$$$$$TechieIndoor$$$", "$") Output: "$$$$$TechieIndoor" 2) s := []byte("\n\nHello World\n\n", "\n") Output: "\n\nHello World" 3) s := []byte("&&TechieIndoor&&", "") Output: "&&TechieIndoor&&" 4) s := []byte("TechieIndoor&&", "&") Output: "TechieIndoor" 5) s := []byte("&&TechieIndoor", "&") Output: "&&TechieIndoor"
Example:
Code 1:
package main import ( "bytes" "fmt" ) // main function func main() { // trim "$" at right the side s := []byte("$$$$$TechieIndoor$$$") fmt.Printf("%q", bytes.TrimRight(s, "$")) // trim "\n" at right the side s = []byte("\n\nHello World\n\n") fmt.Printf("\n%q", bytes.TrimRight(s, "\n")) // trim "" at right the side s = []byte("&&TechieIndoor&&") fmt.Printf("\n%q", bytes.TrimRight(s, "")) // trim "&" at right the side s = []byte("TechieIndoor&&") fmt.Printf("\n%q", bytes.TrimRight(s, "&")) // trim "&" at right the side s = []byte("&&TechieIndoor") fmt.Printf("\n%q", bytes.TrimRight(s, "&")) }
Output:
"$$$$$TechieIndoor" "\n\nHello World" "&&TechieIndoor&&" "TechieIndoor" "&&TechieIndoor"
To learn more about golang, Please refer given below link:
References: