Skip to content

Commit

Permalink
Merge pull request #8 from tainguyenbp/feat/learning-hacking-with-gol…
Browse files Browse the repository at this point in the history
…ang03

test defer
  • Loading branch information
tainguyenbp authored Jun 3, 2024
2 parents fe04e1a + 9eca3f2 commit 3c08c8e
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
14 changes: 14 additions & 0 deletions hacking-go/learn04/defer/defer.go
Original file line number Diff line number Diff line change
@@ -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.
}
40 changes: 40 additions & 0 deletions hacking-go/learn04/defer/defer1.go
Original file line number Diff line number Diff line change
@@ -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()

}
59 changes: 59 additions & 0 deletions hacking-go/learn04/defer/defer2.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions hacking-go/learn04/defer/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
defer
1 change: 1 addition & 0 deletions hacking-go/learn04/defer/tainguyenbp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
Empty file.

0 comments on commit 3c08c8e

Please sign in to comment.