Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang
communitycreator
fieldsfunc()

What is the FieldsFunc() function in Golang?

Faraz Karim

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The FieldsFunc() function in Golang splits a string into a list of substrings on each run of Unicode points. These Unicode points are found in the string that satisfies the function you give as another input.

The passed string is broken down into smaller substrings wherever one or more Unicode characters appear.

How to use the FieldsFunc function

To use this function, you must import the strings package in your file and access the FieldsFunc function within it using the . notation (string.FieldsFunc).

Here, FieldsFunc is the actual function, while string is the Go package that stores the function definition.

Syntax

The definition of the FieldsFunc function inside the string package is as follows:


func FieldsFunc(str string, fun func(rune) bool) []string

Parameters

The FieldsFunc function takes two arguments:

  1. str: This argument is of the string type and represents the text you want to split.

  2. fun: This argument represents a function that you send to the FieldsFunc function.

This function must accept a single argument of type Rune (basically a single character) and return a boolean value.

FieldsFunc makes no guarantees about the order in which it calls fun(chr).

FieldsFunc assumes that fun(chr) is deterministic, meaning it returns the same value every time for the same value of the Unicode character chr passed to it.

Return value

The FieldsFunc function returns an array (of the string type) made up of the substrings from the original string.

The function does so by splitting the string on all instances of Unicode that also satisfy the input function, fun().

Special cases for return value

Special return cases include:

  • When there are no Unicode characters (which satisfy the input function, fun()) in the string, the whole string is returned as the only array member.

  • When a string is empty, or all the characters satisfy the input function fun(), an empty array is returned.

Code

Example 1

In the example below, we split a string on the characters a and e and print the resultant array of the sliced string.

package main
import (
"fmt"
"strings"
)
func main() {
fun := func(chr rune) bool {
return chr == 'a' || chr == 'e'
}
x:=strings.FieldsFunc("I like saying bazinga and bingo", fun)
fmt.Printf("%q", x)
}

Example 2

In the example below, we split a string on the characters a and e and print the resultant array of the sliced string but this time both the characters a and e are not present in the text.

package main
import (
"fmt"
"strings"
)
func main() {
fun := func(chr rune) bool {
return chr == 'a' || chr == 'e'
}
x:=strings.FieldsFunc("Boom boom boom", fun)
fmt.Printf("%q", x)
}

Example 3

The following example shows the return value when you pass a string composed only of characters that satisfy the accompanying function (string with only a and e) to the FieldsFunc function.

package main
import (
"fmt"
"strings"
)
func main() {
fun := func(chr rune) bool {
return chr == 'a' || chr == 'e'
}
x:=strings.FieldsFunc("aeaeaeaeaaaaeeee", fun)
fmt.Printf("%q", x)
}

RELATED TAGS

golang
communitycreator
fieldsfunc()

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring