Standard library / bytes

bytes.Fields()

Splits on runs of Unicode whitespace, discarding empty fields. Returned subslices may alias input storage; copy before retaining independently. Expected stdout: '[a|b]\n'.

Formal Syntax & Structure

go syntaxgo
func Fields(s []byte) [][]byte

Parameters & Attributes

NameTypeDescription
s[]byteInput text or sequence.

Return Value

[][]byte

Splits on runs of Unicode whitespace, discarding empty fields. Returned subslices may alias input storage; copy before retaining independently.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt")
func main() {  v := bytes.Fields([]byte(" a\t b "))  fmt.Print("[")  for i, x := range v {    if i > 0 {      fmt.Print("|")    }    fmt.Print(string(x))  }  fmt.Println("]")}
Go Reference