Standard library / bytes

bytes.SplitN()

Splits 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 SplitN(s, sep []byte, n int) [][]byte

Parameters & Attributes

NameTypeDescription
s[]byteInput text or sequence.
sep[]byteSeparator or search sequence.
nintMaximum result count or number of replacements, as specified.

Return Value

[][]byte

Splits 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.SplitN([]byte("a,b,c"), []byte(","), 2)  fmt.Print("[")  for i, x := range v {    if i > 0 {      fmt.Print("|")    }    fmt.Print(string(x))  }  fmt.Println("]")}
Go Reference