Skip to content

Commit 3c08c8e

Browse files
authored
Merge pull request #8 from tainguyenbp/feat/learning-hacking-with-golang03
test defer
2 parents fe04e1a + 9eca3f2 commit 3c08c8e

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

hacking-go/learn04/defer/defer.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
package main
3+
4+
import "fmt"
5+
6+
func main() {
7+
// This line will be executed immediately.
8+
defer fmt.Println("This runs after main")
9+
10+
// This line will be executed immediately.
11+
fmt.Println("Main ended")
12+
// After the surrounding function returns,
13+
// the deferred function will execute.
14+
}

hacking-go/learn04/defer/defer1.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func defer1_advance() {
6+
num := 100
7+
8+
// First deferred function capturing the value of num at this point
9+
defer func(n int) {
10+
fmt.Println("Deferred 1: After main returns", n) // This will print "Deferred 1: After main returns 110" after main() returns.
11+
}(num)
12+
13+
num++ // num is incremented to 101
14+
15+
// Second deferred function capturing the updated value of num after the first increment
16+
defer func(n int) {
17+
fmt.Println("Deferred 2: After main returns", n) // This will print "Deferred 2: After main returns 101" after main() returns.
18+
}(num)
19+
20+
num += 9 // num is now 110
21+
22+
fmt.Println("Inside main", num) // Prints "Inside main 110"
23+
}
24+
25+
func defer1() {
26+
27+
num := 100
28+
defer fmt.Println("After main returns", num)
29+
30+
num++
31+
num += 9
32+
fmt.Println("Inside main", num)
33+
34+
}
35+
36+
func main() {
37+
defer1()
38+
defer1_advance()
39+
40+
}

hacking-go/learn04/defer/defer2.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
func openfile() {
9+
// Open a file
10+
file, err := os.Open("tainguyenbp.txt")
11+
if err != nil {
12+
fmt.Println("Error:", err)
13+
return
14+
}
15+
16+
// Defer the closing of the file
17+
defer file.Close()
18+
19+
// Perform operations on the file
20+
// For demonstration, let's just print its contents
21+
fmt.Println("File contents:")
22+
data := make([]byte, 100)
23+
count, err := file.Read(data)
24+
if err != nil {
25+
fmt.Println("Error reading file:", err)
26+
return
27+
}
28+
fmt.Println(string(data[:count]))
29+
}
30+
31+
func openfileno() {
32+
// Open a file
33+
file, err := os.Open("tainguyenbp1.txt")
34+
if err != nil {
35+
fmt.Println("Error:", err)
36+
return
37+
}
38+
39+
// Defer the closing of the file
40+
defer file.Close()
41+
42+
// Perform operations on the file
43+
// For demonstration, let's just print its contents
44+
fmt.Println("File contents:")
45+
data := make([]byte, 100)
46+
count, err := file.Read(data)
47+
if err != nil {
48+
fmt.Println("Error reading file:", err)
49+
return
50+
}
51+
fmt.Println(string(data[:count]))
52+
}
53+
54+
func main() {
55+
56+
openfile()
57+
openfileno()
58+
// Any other operations on the file can go here
59+
}

hacking-go/learn04/defer/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
defer
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

hacking-go/learn04/defer/tainguyenbp1.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)