diff --git a/hacking-go/learn04/defer/defer.go b/hacking-go/learn04/defer/defer.go new file mode 100644 index 0000000..d6599ca --- /dev/null +++ b/hacking-go/learn04/defer/defer.go @@ -0,0 +1,14 @@ + +package main + +import "fmt" + +func main() { + // This line will be executed immediately. + defer fmt.Println("This runs after main") + + // This line will be executed immediately. + fmt.Println("Main ended") + // After the surrounding function returns, + // the deferred function will execute. +} diff --git a/hacking-go/learn04/defer/defer1.go b/hacking-go/learn04/defer/defer1.go new file mode 100644 index 0000000..1fcc59d --- /dev/null +++ b/hacking-go/learn04/defer/defer1.go @@ -0,0 +1,40 @@ +package main + +import "fmt" + +func defer1_advance() { + num := 100 + + // First deferred function capturing the value of num at this point + defer func(n int) { + fmt.Println("Deferred 1: After main returns", n) // This will print "Deferred 1: After main returns 110" after main() returns. + }(num) + + num++ // num is incremented to 101 + + // Second deferred function capturing the updated value of num after the first increment + defer func(n int) { + fmt.Println("Deferred 2: After main returns", n) // This will print "Deferred 2: After main returns 101" after main() returns. + }(num) + + num += 9 // num is now 110 + + fmt.Println("Inside main", num) // Prints "Inside main 110" +} + +func defer1() { + + num := 100 + defer fmt.Println("After main returns", num) + + num++ + num += 9 + fmt.Println("Inside main", num) + +} + +func main() { + defer1() + defer1_advance() + +} diff --git a/hacking-go/learn04/defer/defer2.go b/hacking-go/learn04/defer/defer2.go new file mode 100644 index 0000000..6b89eb0 --- /dev/null +++ b/hacking-go/learn04/defer/defer2.go @@ -0,0 +1,59 @@ +package main + +import ( + "fmt" + "os" +) + +func openfile() { + // Open a file + file, err := os.Open("tainguyenbp.txt") + if err != nil { + fmt.Println("Error:", err) + return + } + + // Defer the closing of the file + defer file.Close() + + // Perform operations on the file + // For demonstration, let's just print its contents + fmt.Println("File contents:") + data := make([]byte, 100) + count, err := file.Read(data) + if err != nil { + fmt.Println("Error reading file:", err) + return + } + fmt.Println(string(data[:count])) +} + +func openfileno() { + // Open a file + file, err := os.Open("tainguyenbp1.txt") + if err != nil { + fmt.Println("Error:", err) + return + } + + // Defer the closing of the file + defer file.Close() + + // Perform operations on the file + // For demonstration, let's just print its contents + fmt.Println("File contents:") + data := make([]byte, 100) + count, err := file.Read(data) + if err != nil { + fmt.Println("Error reading file:", err) + return + } + fmt.Println(string(data[:count])) +} + +func main() { + + openfile() + openfileno() + // Any other operations on the file can go here +} diff --git a/hacking-go/learn04/defer/readme.md b/hacking-go/learn04/defer/readme.md new file mode 100644 index 0000000..4bd4a8e --- /dev/null +++ b/hacking-go/learn04/defer/readme.md @@ -0,0 +1 @@ +defer diff --git a/hacking-go/learn04/defer/tainguyenbp.txt b/hacking-go/learn04/defer/tainguyenbp.txt new file mode 100644 index 0000000..9daeafb --- /dev/null +++ b/hacking-go/learn04/defer/tainguyenbp.txt @@ -0,0 +1 @@ +test diff --git a/hacking-go/learn04/defer/tainguyenbp1.txt b/hacking-go/learn04/defer/tainguyenbp1.txt new file mode 100644 index 0000000..e69de29