What is the IsOneOf() function in Golang?

Golang IsOneOf() function

The IsOneOf() function is a built-in function of the Unicode package that determines whether the given rune r belongs to one of the specified range tables.

Syntax

func IsOneOf(ranges []*RangeTable, r rune) bool

Parameters

ranges: A table with one or more ranges.

r: The rune type value we want to see if it is a member of one of the ranges.

Return value

The Unicode.IsOneOf() function returns true if rune r is a member of one of the ranges. Otherwise, it returns false.

Note: We must import the Unicode package into our program to use the IsOneOf() function.

package main
  
import (
    "fmt"
    "unicode"
)

Code

The following code shows how to use the Unicode.IsOneOf() function in Golang.

// Golang program
// Using unicode.IsOneOf() Function
package main
import (
"fmt"
"unicode"
)
func main() {
// declare rangeTable
// contains letter and digits
var letterDigit = []*unicode.RangeTable{
unicode.Letter,
unicode.Digit,
{R16: []unicode.Range16{{'_', '_', 1}}},
}
// declaring a constant str with type runes
const str = "Edu5Ὂca?tive@~Shot℃ᾭ&*"
// checks each character whether it is within the rangeTable
// result is displayed based on the return value
for _, i := range str {
if unicode.IsOneOf(letterDigit, i) == true {
fmt.Printf("%c is in the range\n", i)
} else {
fmt.Printf("%c is not the range\n",i)
}
}
}

The following is another code example that checks if each character is in the string (rune type).

// Golang program
// Using unicode.IsOneOf() Function
package main
import (
"fmt"
"unicode"
)
func main() {
// declare rangeTable
// contains letter and digits
var letterDigit = []*unicode.RangeTable{
unicode.Letter,
unicode.Digit,
{R16: []unicode.Range16{{'_', '_', 1}}},
}
// declaring a constant str with type runes
const str = "Edu5Ὂca?tive@~Shot℃ᾭ&*"
// checks each character whether it is within the rangeTable
// result is displayed based on the return value
for _, i := range str {
if unicode.IsOneOf(letterDigit, i) == true {
fmt.Printf("%c is in the range\n", i)
} else {
fmt.Printf("%c is not the range\n",i)
}
}
}

Free Resources