publish number-parsing
This commit is contained in:
parent
27f79cd6cb
commit
f8ed65b151
@ -52,8 +52,8 @@ Defer
|
|||||||
# Epochs
|
# Epochs
|
||||||
# Elapsed Time
|
# Elapsed Time
|
||||||
# Random Numbers
|
# Random Numbers
|
||||||
# Number Parsing
|
Number Parsing
|
||||||
URLs
|
URL Parsing
|
||||||
SHA1 Hashes
|
SHA1 Hashes
|
||||||
Base64 Encoding
|
Base64 Encoding
|
||||||
# Reading Files
|
# Reading Files
|
||||||
|
@ -1,29 +1,42 @@
|
|||||||
|
// Parsing numbers from strings is a basic but common task
|
||||||
|
// in many programs; here's how to do it in Go.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
// Package `strconv` provides the number parsing.
|
// The built-in package `strconv` provides the number
|
||||||
|
// parsing.
|
||||||
import "strconv"
|
import "strconv"
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// `64` tells how many bits of precision to parse.
|
|
||||||
|
// With `ParseFloat`, this `64` tells how many bits of
|
||||||
|
// precision to parse.
|
||||||
f, _ := strconv.ParseFloat("1.234", 64)
|
f, _ := strconv.ParseFloat("1.234", 64)
|
||||||
fmt.Println(f)
|
fmt.Println(f)
|
||||||
|
|
||||||
// `0` means infer the base from the string.
|
// For `ParseInt`, the `0` means infer the base from
|
||||||
// `64` requires that the result fit in 64 bits.
|
// the string. `64` requires that the result fit in 64
|
||||||
|
// bits.
|
||||||
i, _ := strconv.ParseInt("123", 0, 64)
|
i, _ := strconv.ParseInt("123", 0, 64)
|
||||||
println(i)
|
fmt.Println(i)
|
||||||
|
|
||||||
// `ParseInt` will recognize hex-formatted numbers.
|
// `ParseInt` will recognize hex-formatted numbers.
|
||||||
d, _ := strconv.ParseInt("0x1b3e", 0, 64)
|
d, _ := strconv.ParseInt("0x1c8", 0, 64)
|
||||||
println(d)
|
fmt.Println(d)
|
||||||
|
|
||||||
// `Atoi` is a convenienice function for `int`
|
// A `ParseUint` is also available.
|
||||||
// parsing.
|
u, _ := strconv.ParseUint("789", 0, 64)
|
||||||
k, _ := strconv.Atoi("456")
|
fmt.Println(u)
|
||||||
println(k)
|
|
||||||
|
// `Atoi` is a convenience function for basic base-10
|
||||||
|
// `int` parsing.
|
||||||
|
k, _ := strconv.Atoi("135")
|
||||||
|
fmt.Println(k)
|
||||||
|
|
||||||
// Parse functions return an error on bad input.
|
// Parse functions return an error on bad input.
|
||||||
_, e := strconv.Atoi("wat")
|
_, e := strconv.Atoi("wat")
|
||||||
fmt.Println(e)
|
fmt.Println(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Next we'll look at another common parsing task: URLs.
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
$ go run xx-number-parsing.go
|
$ go run number-parsing.go
|
||||||
1.234
|
1.234
|
||||||
123
|
123
|
||||||
6974
|
|
||||||
456
|
456
|
||||||
|
789
|
||||||
|
135
|
||||||
strconv.ParseInt: parsing "wat": invalid syntax
|
strconv.ParseInt: parsing "wat": invalid syntax
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# Running our URL parsing program shows all the different
|
# Running our URL parsing program shows all the different
|
||||||
# pieces that we extracted.
|
# pieces that we extracted.
|
||||||
$ go run urls.go
|
$ go run url-parsing.go
|
||||||
postgres
|
postgres
|
||||||
user:pass
|
user:pass
|
||||||
user
|
user
|
Loading…
x
Reference in New Issue
Block a user