Standard library / bytes
bytes.SplitAfterN()
Splits after separators into at most n pieces; n=0 returns nil and n<0 is unlimited. Returned subslices may alias input storage; copy before retaining independently. Expected stdout: '[a,|b,c]\n'.
Formal Syntax & Structure
go syntaxgo
func SplitAfterN(s, sep []byte, n int) [][]byteParameters & Attributes
| Name | Type | Description |
|---|---|---|
| s | []byte | Input text or sequence. |
| sep | []byte | Separator or search sequence. |
| n | int | Maximum result count or number of replacements, as specified. |
Return Value
[][]byte
Splits after separators into at most n pieces; n=0 returns nil and n<0 is unlimited. Returned subslices may alias input storage; copy before retaining independently.
Working Example
Example Snippetgo
package main
import ( "bytes" "fmt")
func main() { v := bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2) fmt.Print("[") for i, x := range v { if i > 0 { fmt.Print("|") } fmt.Print(string(x)) } fmt.Println("]")}