-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
defer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test |
Empty file.