1.XML
Go offers built-in support for XML and XML-like formats with the encoding.xml
package.
[maxwell@oracle-db-19c Day06]$ vim xml.go
[maxwell@oracle-db-19c Day06]$ cat xml.go
package main
import (
"encoding/xml"
"fmt"
)
type Plant struct {
XMLName xml.Name `xml:"plant"`
Id int `xml:"id,attr"`
Name string `xml:"name"`
Origin []string `xml:"origin"`
}
func (p Plant) String() string {
return fmt.Sprintf("Plant id=%v, name=%v, origin=%v",p.Id, p.Name, p.Origin)
}
func main() {
coffee := &Plant{Id: 27, Name: "Coffee"}
coffee.Origin = []string{"Ethiopia","Brazil"}
out,_ := xml.MarshalIndent(coffee, " ", " ")
fmt.Println(string(out))
fmt.Println(xml.Header + string(out))
var p Plant
if err := xml.Unmarshal(out,&p);err!= nil {
panic(err)
}
fmt.Println(p)
tomato := &Plant{Id: 81, Name: "Tomato"}
tomato.Origin = []string{"Mexico", "California"}
type Nesting struct {
XMLName xml.Name `xml:"nesting"`
Plants []*Plant `xml:"Parent>child>plant"`
}
nesting := &Nesting{}
nesting.Plants = []*Plant{coffee, tomato}
out,_ = xml.MarshalIndent(nesting, " ", " ")
fmt.Println(string(out))
}
[maxwell@oracle-db-19c Day06]$ go run xml.go
<plant id="27">
<name>Coffee</name>
<origin>Ethiopia</origin>
<origin>Brazil</origin>
</plant>
<?xml version="1.0" encoding="UTF-8"?>
<plant id="27">
<name>Coffee</name>
<origin>Ethiopia</origin>
<origin>Brazil</origin>
</plant>
Plant id=27, name=Coffee, origin=[Ethiopia Brazil]
<nesting>
<Parent>
<child>
<plant id="27">
<name>Coffee</name>
<origin>Ethiopia</origin>
<origin>Brazil</origin>
</plant>
<plant id="81">
<name>Tomato</name>
<origin>Mexico</origin>
<origin>California</origin>
</plant>
</child>
</Parent>
</nesting>
[maxwell@oracle-db-19c Day06]$
2. Time
Go offers extensive support for times and durations; here are some examples. |
[maxwell@oracle-db-19c Day06]$ vim time.go
[maxwell@oracle-db-19c Day06]$ cat time.go
package main
import (
"fmt"
"time"
)
func main(){
p := fmt.Println
now := time.Now()
p(now)
then := time.Date(
2023, 02, 22, 10, 43, 58, 651387237, time.UTC)
p(then)
p(then.Year())
p(then.Month())
p(then.Day())
p(then.Hour())
p(then.Minute())
p(then.Second())
p(then.Nanosecond())
p(then.Location())
p(then.Weekday())
p(then.Before(now))
p(then.After(now))
p(then.Equal(now))
diff := now.Sub(then)
p(diff)
p(diff.Hours())
p(diff.Minutes())
p(diff.Seconds())
p(diff.Nanoseconds())
p(then.Add(diff))
p(then.Add(-diff))
}
[maxwell@oracle-db-19c Day06]$ go run time.go
2023-02-22 10:44:08.274731883 +0800 HKT m=+0.000033700
2023-02-22 10:43:58.651387237 +0000 UTC
2023
February
22
10
43
58
651387237
UTC
Wednesday
false
true
false
-7h59m50.376655354s
-7.997326848709444
-479.8396109225667
-28790.376655354
-28790376655354
2023-02-22 02:44:08.274731883 +0000 UTC
2023-02-22 18:43:49.028042591 +0000 UTC
[maxwell@oracle-db-19c Day06]$
3. Epoch
A common requirement in programs is getting the number of seconds, milliseconds, or nanoseconds since the Unix epoch. Here’s how to do it in Go.
[maxwell@oracle-db-19c Day06]$ vim epoch.go
[maxwell@oracle-db-19c Day06]$ cat epoch.go
package main
import (
"fmt"
"time"
)
func main(){
now := time.Now()
fmt.Println(now)
fmt.Println(now.Unix())
fmt.Println(now.UnixMilli())
fmt.Println(now.UnixNano())
fmt.Println(time.Unix(now.Unix(),0))
fmt.Println(time.Unix(0, now.UnixNano()))
}
[maxwell@oracle-db-19c Day06]$ go run epoch.go
2023-02-22 10:54:33.619707882 +0800 HKT m=+0.000033672
1677034473
1677034473619
1677034473619707882
2023-02-22 10:54:33 +0800 HKT
2023-02-22 10:54:33.619707882 +0800 HKT
[maxwell@oracle-db-19c Day06]$
4.Time Formatting / Parsing
[maxwell@oracle-db-19c Day06]$ vim timeformatting_parsing.go
[maxwell@oracle-db-19c Day06]$ cat timeformatting_parsing.go
package main
import (
"fmt"
"time"
)
func main() {
p := fmt.Println
t := time.Now()
p(t.Format(time.RFC3339))
t1, e := time.Parse(
time.RFC3339,"2023-02-22T14:09:41+00:00")
p(t1)
p(t.Format("2:10PM"))
p(t.Format("Mon Feb _22 14:04:05 2023"))
p(t.Format("2023-02-22T14:04:05.999999-07:00"))
form := "3 04 PM"
t2, e := time.Parse(form, "8 41 PM")
p(t2)
fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",t.Year(), t.Month(), t.Day(),t.Hour(), t.Minute(), t.Second())
ansic := "Wed Feb _22 14:04:05 2023"
_, e = time.Parse(ansic, "8:41PM")
p(e)
}
[maxwell@oracle-db-19c Day06]$ go run timeformatting_parsing.go
2023-02-22T14:18:02+08:00
2023-02-22 14:09:41 +0000 +0000
22:20PM
Wed Feb 2222 218:18:02 22222
22222-22-2222T218:18:02.505792+08:00
0000-01-01 20:41:00 +0000 UTC
2023-02-22T14:18:02-00:00
parsing time "8:41PM" as "Wed Feb _22 14:04:05 2023": cannot parse "8:41PM" as "Wed Feb "
[maxwell@oracle-db-19c Day06]$
5.Random Numbers
Go’s math/rand package provides pseudorandom number generation.
[maxwell@oracle-db-19c Day06]$ vim random_numbers.go
[maxwell@oracle-db-19c Day06]$ cat random_numbers.go
package main
import (
"fmt"
"math/rand"
"time"
)
func main(){
fmt.Print(rand.Intn(100), ".")
fmt.Print(rand.Intn(100))
fmt.Println()
fmt.Println(rand.Float64())
fmt.Print((rand.Float64()*5)+5, ",")
fmt.Print((rand.Float64() * 5) + 5)
fmt.Println()
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
fmt.Print(r1.Intn(100), ",")
fmt.Print(r1.Intn(100))
fmt.Println()
s2 := rand.NewSource(42)
r2 := rand.New(s2)
fmt.Print(r2.Intn(100),",")
fmt.Print(r2.Intn(100))
fmt.Println()
s3 := rand.NewSource(42)
r3 := rand.New(s3)
fmt.Print(r3.Intn(100),",")
fmt.Print(r3.Intn(100))
fmt.Println()
}
[maxwell@oracle-db-19c Day06]$ go run random_numbers.go
81.87
0.6645600532184904
7.1885709359349015,7.123187485356329
10,58
5,87
5,87
[maxwell@oracle-db-19c Day06]$