Golang strconv.ErrRange Variable with Examples

Golang | strconv.ErrRange Variable: Here, we are going to learn about the ErrRange variable of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 05, 2021

strconv.ErrRange Variable

The ErrRange variable is an inbuilt variable of the strconv package which indicates that a value is out of range for the target type. The value of the ErrRange variable is "value out of range".

Syntax:

*errors.errorString strconv.ErrRange
var ErrRange = errors.New("value out of range")

Parameter(s):

  • None

Return Value:

The return type of strconv.ErrRange variable is a *errors.errorString, it returns the string "value out of range".

Example 1:

// Golang program to demonstrate the
// example of strconv.ErrRange Variable

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Printf("Type of strconv.ErrRange is %T\n", strconv.ErrRange)
	fmt.Println("Value of strconv.ErrRange:", strconv.ErrRange)
}

Output:

Type of strconv.ErrRange is *errors.errorString
Value of strconv.ErrRange: value out of range

Explanation:

In the above program, we imported the strconv package to use the strconv.ErrRange variable, then printed the type and value of the strconv.ErrRange variable.

Example 2:

// Golang program to demonstrate the
// example of strconv.ErrRange Variable

package main

import (
	"fmt"
	"strconv"
)

func main() {
	result, err := strconv.ParseInt("18446744073729762559", 10, 64)
	if err != nil {
		if numErr, ok := err.(*strconv.NumError); ok {
			if numErr.Err == strconv.ErrRange {
				fmt.Println("Error:", numErr.Num, "as a", strconv.ErrRange)
				return
			}
		}
		fmt.Println(err)
		return
	}
	fmt.Println(result)
}

Output:

Error: 18446744073729762559 as a value out of range

Golang strconv Package »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.