bytes.Cut Splits At The First Separator
Split a byte slice once with bytes.Cut and walk repeated delimiters, receiving before, after, and an explicit found flag.
Code
package main
import (
"bytes"
"fmt"
)
func main() {
// Cut 一次切分:before + 分隔符 + after,外加显式 found 标志
header, body, found := bytes.Cut([]byte("token=abc123;expires=3600"), []byte(";"))
fmt.Printf("found=%v header=%q body=%q\n", found, header, body)
// 循环 Cut 解析键值对,直到没有分隔符(找不到时 after 为 nil)
rest := []byte("a=1,b=2,c=3")
for len(rest) > 0 {
pair, tail, ok := bytes.Cut(rest, []byte(","))
fmt.Printf("pair=%q\n", pair)
if !ok {
break
}
rest = tail
}
}What the source does next
bytes.Cut Splits At The First Separator comes down to the function main.
A single loop drives the file — for len(rest) > 0.
What to watch for
- No external packages — everything relies on the language’s built-in APIs.
Using it
Signature: func main()
Running the file prints labeled results such as found=, pair= — each operation’s effect is visible in the output.
Key statements
header, body, found := bytes.Cut([]byte("token=abc123;expires=3600"), []byte(";"))
Interactive Tools for This Topic
Byte Size ConverterShow one byte count in every unit side by side, using decimal (kB/MB/GB) or binary (KiB/MiB/GiB) standards.cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.Hex Dump ViewerPreview any text as a classic hexadecimal dump with byte offsets, sixteen bytes per row, and an ASCII column for quick inspection.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- What does bytes.Cut Splits At The First Separator print when run?
- The script runs standalone; its 2 labeled outputs include `found=`, `pair=`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.