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.

GoData Integrity

Code

gosnippet
23 lines
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

FAQ

What does bytes.Cut Splits At The First Separator print when run?
The script runs standalone; its 2 labeled outputs include `found=`, `pair=`.