Skip to content

Commit 3ba7079

Browse files
committed
better time and file examples
1 parent a5a16ee commit 3ba7079

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

beginner/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import "os"
44

5-
// OpenCreateFile open an existed file or create a file if not exists
5+
// OpenCreateFile opens an existing file or creates a new file if it does not exist
66
func OpenCreateFile(path string) *os.File {
77
if _, err := os.Stat(path); os.IsNotExist(err) {
88
f, err := os.Create(path)

beginner/file02.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
)
8+
9+
func write() {
10+
content := []byte("Hello, Golang!")
11+
err := ioutil.WriteFile("example.txt", content, 0644)
12+
if err != nil {
13+
log.Fatal(err)
14+
}
15+
}
16+
17+
func read() {
18+
content, err := ioutil.ReadFile("example.txt")
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
23+
fmt.Printf("File content: %s\n", content)
24+
}
25+
26+
func main() {
27+
write()
28+
read()
29+
}

beginner/time.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
)
88

99
func main() {
10+
start := time.Now()
11+
fmt.Println("Current time:", start)
12+
//Current time: 2023-04-05 19:43:23.329968 +0200 CEST m=+0.000140960
13+
1014
fmt.Println(strconv.FormatInt(time.Now().Unix(), 10))
1115
//1401403874
1216
fmt.Println(time.Now().Format(time.RFC850))
@@ -21,4 +25,8 @@ func main() {
2125
//1973-11-29T22:33:09+01:00
2226
fmt.Println(time.Unix(1234567890, 0).Format(time.RFC822))
2327
//14 Feb 09 00:31 CET
28+
29+
elapsed := time.Since(start)
30+
fmt.Println("Time elapsed:", elapsed)
31+
//Time elapsed: 428.458µs
2432
}

0 commit comments

Comments
 (0)