Last active
July 26, 2024 16:26
-
-
Save arya2004/934eefabdd87f3c621f6bff1923bc5a9 to your computer and use it in GitHub Desktop.
Understanding Functions, Recursion, and Defer in Go
This file contains hidden or 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
| package main | |
| import ( | |
| "fmt" | |
| "os" | |
| ) | |
| func main() { | |
| for i := 0; i < 3; i++ { | |
| file, err := os.Open("example.txt") | |
| if err != nil { | |
| fmt.Println("Error:", err) | |
| return | |
| } | |
| defer file.Close() | |
| } | |
| } |
This file contains hidden or 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
| package main | |
| import ( | |
| "fmt" | |
| "os" | |
| ) | |
| func main() { | |
| for i := 0; i < 3; i++ { | |
| file, err := os.Open("example.txt") | |
| if err != nil { | |
| fmt.Println("Error:", err) | |
| return | |
| } | |
| // Do something with the file | |
| file.Close() | |
| } | |
| } |
This file contains hidden or 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
| package main | |
| import ( | |
| "fmt" | |
| "os" | |
| ) | |
| func main() { | |
| for i := 0; i < 3; i++ { | |
| file, err := os.Open("example.txt") | |
| if err != nil { | |
| fmt.Println("Error:", err) | |
| return | |
| } | |
| defer file.Close() | |
| } | |
| } |
This file contains hidden or 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
| package main | |
| import "fmt" | |
| func main() { | |
| defer fmt.Println("First defer") | |
| defer fmt.Println("Second defer") | |
| defer fmt.Println("Third defer") | |
| fmt.Println("Main function") | |
| } |
This file contains hidden or 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
| package main | |
| import "fmt" | |
| func main() { | |
| defer fmt.Println("World") | |
| fmt.Println("Hello") | |
| } |
This file contains hidden or 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
| package main | |
| import "fmt" | |
| func outer() { | |
| innerVar := 10 | |
| fmt.Println("Inner variable:", innerVar) | |
| innerFunction := func() { | |
| fmt.Println("Inner function called") | |
| } | |
| innerFunction() | |
| } | |
| func main() { | |
| outer() | |
| } |
This file contains hidden or 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
| package main | |
| import "fmt" | |
| func add(a int, b float64) float64 { | |
| return float64(a) + b | |
| } | |
| func subtract(a float64, b int) float64 { | |
| return a - float64(b) | |
| } | |
| func main() { | |
| fmt.Println(add(2, 3.5)) // Output: 5.5 | |
| fmt.Println(subtract(5.5, 2)) // Output: 3.5 | |
| } |
This file contains hidden or 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
| package main | |
| import "fmt" | |
| func main() { | |
| add := func(a, b int) int { | |
| return a + b | |
| } | |
| fmt.Println(add(2, 3)) // Output: 5 | |
| } |
This file contains hidden or 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
| package main | |
| import "fmt" | |
| func modifyArray(arr [3]int) { | |
| arr[0] = 0 | |
| } | |
| func main() { | |
| original := [3]int{1, 2, 3} | |
| modifyArray(original) | |
| fmt.Println(original) // Output: [1, 2, 3] | |
| } |
This file contains hidden or 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
| package main | |
| import "fmt" | |
| func modifyMap(m map[int]int) { | |
| m[1] = 0 | |
| } | |
| func main() { | |
| original := map[int]int{1: 1, 2: 2, 3: 3} | |
| modifyMap(original) | |
| fmt.Println(original) // Output: map[1:0 2:2 3:3] | |
| } |
This file contains hidden or 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
| package main | |
| import ( | |
| "fmt" | |
| "os" | |
| ) | |
| func openFile(filename string) (*os.File, error) { | |
| file, err := os.Open(filename) | |
| return file, err | |
| } | |
| func main() { | |
| file, err := openFile("example.txt") | |
| if err != nil { | |
| fmt.Println("Error:", err) | |
| return | |
| } | |
| defer file.Close() | |
| // Do something with the file | |
| fmt.Println("File opened successfully") | |
| } |
This file contains hidden or 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
| package main | |
| import ( | |
| "fmt" | |
| "os" | |
| ) | |
| func main() { | |
| file, err := os.Open("example.txt") | |
| if err != nil { | |
| fmt.Println("Error:", err) | |
| return | |
| } | |
| defer file.Close() | |
| // Do something with the file | |
| } |
This file contains hidden or 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
| package main | |
| import "fmt" | |
| func multiplier(factor int) func(int) int { | |
| return func(x int) int { | |
| return x * factor | |
| } | |
| } | |
| func main() { | |
| double := multiplier(2) | |
| fmt.Println(double(3)) // Output: 6 | |
| } |
This file contains hidden or 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
| package main | |
| import "fmt" | |
| func add(a, b int) int { | |
| return a + b | |
| } | |
| func main() { | |
| result := add(2, 3) | |
| fmt.Println(result) // Output: 5 | |
| } |
This file contains hidden or 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
| package main | |
| import "fmt" | |
| func modifySlice(slice []int) { | |
| slice[0] = 0 | |
| } | |
| func main() { | |
| original := []int{1, 2, 3} | |
| modifySlice(original) | |
| fmt.Println(original) // Output: [0, 2, 3] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment