Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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.

PreviousRandom NumbersNextSHA256 Hashes

Last updated 1 year ago

Was this helpful?