1.Number Parsing
Parsing numbers from strings is a basic but common task in many programs; here’s how to do it in Go.
The built-in package strconv
provides the number parsing.
[maxwell@oracle-db-19c Day07]$ vim number_parsing.go
[maxwell@oracle-db-19c Day07]$ cat number_parsing.go
package main
import (
"fmt"
"strconv"
)
func main() {
f,_ := strconv.ParseFloat("1.234", 64)
fmt.Println(f)
i,_ := strconv.ParseInt("123", 0, 64)
fmt.Println(i)
d,_ := strconv.ParseInt("0x1c8",0,64)
fmt.Println(d)
u,_ := strconv.ParseUint("789", 0, 64)
fmt.Println(u)
k,_ := strconv.Atoi("135")
fmt.Println(k)
_,e := strconv.Atoi("wat")
fmt.Println(e)
}
[maxwell@oracle-db-19c Day07]$ go run number_parsing.go
1.234
123
456
789
135
strconv.Atoi: parsing "wat": invalid syntax
[maxwell@oracle-db-19c Day07]$
2.URL Parsing
URLs provide a uniform way to locate resources.
We’ll parse this example URL, which includes a scheme, authentication info, host, port, path, query params, and query fragment.
[maxwell@oracle-db-19c Day07]$ vim url-parsing.go
[maxwell@oracle-db-19c Day07]$ cat url-parsing.go
package main
import (
"fmt"
"net"
"net/url"
)
func main(){
s := "postgres://user:pass@host.com:5432/path?k=v#f"
u, err := url.Parse(s)
if err != nil {
panic(err)
}
fmt.Println(u.Scheme)
fmt.Println(u.User)
fmt.Println(u.User.Username())
p,_:= u.User.Password()
fmt.Println(p)
fmt.Println(u.Host)
host, port,_ := net.SplitHostPort(u.Host)
fmt.Println(host)
fmt.Println(port)
fmt.Println(u.Path)
fmt.Println(u.Fragment)
fmt.Println(u.RawQuery)
m,_:= url.ParseQuery(u.RawQuery)
fmt.Println(m)
fmt.Println(m["k"][0])
}
[maxwell@oracle-db-19c Day07]$ go run url-parsing.go
postgres
user:pass
user
pass
host.com:5432
host.com
5432
/path
f
k=v
map[k:[v]]
v
[maxwell@oracle-db-19c Day07]$
3. SHA256 Hashes
SHA256 hashes are frequently used to compute short identities for binary or text blobs. For example, TLS/SSL certificates use SHA256 to compute a certificate’s signature. Here’s how to compute SHA256 hashes in Go.
[maxwell@oracle-db-19c Day07]$ vim sha256-hashes.go
[maxwell@oracle-db-19c Day07]$ cat sha256-hashes.go
package main
import (
"crypto/sha256"
"fmt"
)
func main() {
s := "sha256 this string"
h := sha256.New()
h.Write([]byte(s))
bs := h.Sum(nil)
fmt.Println(s)
fmt.Printf("%x\n", bs)
}
[maxwell@oracle-db-19c Day07]$ go run sha256-hashes.go
sha256 this string
1af1dfa857bf1d8814fe1af8983c18080019922e557f15a8a0d3db739d77aacb
[maxwell@oracle-db-19c Day07]$
4.Base64 Encoding
Go provides built-in support for base64 encoding/decoding.
[maxwell@oracle-db-19c Day07]$ vim base64-encoding.go
[maxwell@oracle-db-19c Day07]$ cat base64-encoding.go
package main
import (
b64 "encoding/base64"
"fmt"
)
func main(){
data := "abc123!?$*&()'-=@~"
sEnc := b64.StdEncoding.EncodeToString([]byte(data))
fmt.Println(sEnc)
sDec,_ := b64.StdEncoding.DecodeString(sEnc)
fmt.Println(string(sDec))
fmt.Println()
uEnc := b64.URLEncoding.EncodeToString([]byte(data))
fmt.Println(uEnc)
uDec,_ := b64.URLEncoding.DecodeString(uEnc)
fmt.Println(string(uDec))
}
[maxwell@oracle-db-19c Day07]$ go run base64-encoding.go
YWJjMTIzIT8kKiYoKSctPUB+
abc123!?$*&()'-=@~
YWJjMTIzIT8kKiYoKSctPUB-
abc123!?$*&()'-=@~
[maxwell@oracle-db-19c Day07]$
5. Reading Files
Reading and writing files are basic tasks needed for many Go programs. First we’ll look at some examples of reading files.
[maxwell@oracle-db-19c Day07]$ vim reading-files.go
[maxwell@oracle-db-19c Day07]$ cat reading-files.go
package main
import (
"bufio"
"fmt"
"io"
"os"
)
func check(e error){
if e != nil {
panic(e)
}
}
func main() {
dat, err := os.ReadFile("/tmp/dat")
check(err)
fmt.Println(string(dat))
f, err := os.Open("/tmp/dat")
check(err)
b1 := make([]byte, 5)
n1, err := f.Read(b1)
check(err)
fmt.Printf("%d bytes: %s\n", n1, string(b1[:n1]))
o2, err := f.Seek(6, 0)
check(err)
b2 := make([]byte, 2)
n2, err := f.Read(b2)
check(err)
fmt.Printf("%d bytes @ %d",n2, o2)
fmt.Printf("%v\n", string(b2[:n2]))
o3, err := f.Seek(6, 0)
check(err)
b3 := make([]byte, 2)
n3, err := io.ReadAtLeast(f, b3, 2)
check(err)
fmt.Printf("%d bytes @ %d: %s\n", n3, o3, string(b3))
_, err = f.Seek(0, 0)
check(err)
r4 := bufio.NewReader(f)
b4, err := r4.Peek(5)
check(err)
fmt.Printf("5 bytes: %s\n", string(b4))
f.Close()
}
[maxwell@oracle-db-19c Day07]$ echo "hello" > /tmp/dat
[maxwell@oracle-db-19c Day07]$ echo "go" >> /tmp/dat
[maxwell@oracle-db-19c Day07]$ go run reading-files.go
hello
go
5 bytes: hello
2 bytes @ 6go
2 bytes @ 6: go
5 bytes: hello
[maxwell@oracle-db-19c Day07]$