Number Parsing

This Go code demonstrates the use of the strconv package for converting strings to various numeric types. Let's go through the code with inline comments and explanations:

// Importing necessary packages.
import (
	"fmt"
	"strconv"
)

// The main function, where the execution of the program begins.
func main() {
	// Parsing a floating-point number from a string.
	f, _ := strconv.ParseFloat("1.234", 64)
	fmt.Println(f)

	// Parsing an integer from a string.
	i, _ := strconv.ParseInt("123", 0, 64)
	fmt.Println(i)

	// Parsing a hexadecimal integer from a string.
	d, _ := strconv.ParseInt("0x1c8", 0, 64)
	fmt.Println(d)

	// Parsing an unsigned integer from a string.
	u, _ := strconv.ParseUint("789", 0, 64)
	fmt.Println(u)

	// Parsing an integer from a string using Atoi.
	k, _ := strconv.Atoi("135")
	fmt.Println(k)

	// Attempting to parse an invalid string to an integer (results in an error).
	_, e := strconv.Atoi("wat")
	fmt.Println(e)
}

Output

1.234
123
456
789
135
strconv.Atoi: parsing "wat": invalid syntax

Explanation:

  1. Parsing a Floating-Point Number:

    • strconv.ParseFloat("1.234", 64) parses the string "1.234" to a floating-point number with a precision of 64 bits.

  2. Parsing an Integer:

    • strconv.ParseInt("123", 0, 64) parses the string "123" to a signed integer with a bit size of 64.

  3. Parsing a Hexadecimal Integer:

    • strconv.ParseInt("0x1c8", 0, 64) parses the string "0x1c8" as a hexadecimal integer with a bit size of 64.

  4. Parsing an Unsigned Integer:

    • strconv.ParseUint("789", 0, 64) parses the string "789" to an unsigned integer with a bit size of 64.

  5. Parsing Integer using Atoi:

    • strconv.Atoi("135") is a shorthand for parsing an integer using ParseInt with base 10 and bit size 64.

  6. Parsing an Invalid String (Results in Error):

    • strconv.Atoi("wat") attempts to parse the invalid string "wat" to an integer, resulting in an error.

This code demonstrates the use of strconv functions for converting strings to numeric types in Go. It also shows handling errors when parsing invalid strings.

Last updated