Golang strconv.ErrSyntax Variable with Examples

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

strconv.ErrSyntax Variable

The ErrSyntax variable is an inbuilt variable of the strconv package which indicates that a value does not have the right syntax for the target type. The value of the ErrSyntax variable is "invalid syntax".

Syntax:

*errors.errorString strconv.ErrSyntax
var ErrSyntax = errors.New("invalid syntax")

Parameter(s):

  • None

Return Value:

The return type of strconv.ErrSyntax variable is a *errors.errorString, it returns the string "invalid syntax".

Example 1:

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

package main

import (
	"fmt"
	"strconv"
)

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

Output:

Type of strconv.ErrSyntax is *errors.errorString
Value of strconv.ErrSyntax: invalid syntax

Explanation:

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

Example 2:

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

package main

import (
	"errors"
	"fmt"
	"os"
	"strconv"
)

func main() {
	b := "123@okay"

	a, err := strconv.Atoi(b)

	if errors.Is(err, strconv.ErrSyntax) {
		fmt.Println("Error Detected:", strconv.ErrSyntax)
		os.Exit(1)
	}

	fmt.Println(a)
}

Output:

Error Detected: invalid syntax

Golang strconv Package »





Comments and Discussions!

Load comments ↻






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