Skip to content

Instantly share code, notes, and snippets.

@monkrus
Last active November 23, 2022 18:23
Show Gist options
  • Save monkrus/60208dfa46f29fb7456805c3dd977782 to your computer and use it in GitHub Desktop.
Save monkrus/60208dfa46f29fb7456805c3dd977782 to your computer and use it in GitHub Desktop.
Golang byte trimming
package main
import (
"bytes"
"fmt"
"strings"
)
func main() {
var s string = "Hello World"
sb := []byte(s)
sba := string([]byte(sb))
sbb := bytes.TrimSuffix(sb, []byte{100})
sbc := strings.TrimPrefix(s, "Hello ")
fmt.Printf("The result is %s \n ", sb) // Hello World
fmt.Printf("The result is %q \n ", sba) // [72 101 108 108 111 32 87 111 114 108 100]
fmt.Printf("The result is %s \n ", sbb) // [72 101 108 108 111 32 87 111 114 108]
fmt.Printf("The result is %q \n ", sbc) // World
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment