Last active
November 23, 2022 18:23
-
-
Save monkrus/60208dfa46f29fb7456805c3dd977782 to your computer and use it in GitHub Desktop.
Golang byte trimming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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