├── .gitignore ├── 1.Introduction ├── README.md └── introduction.go ├── 2.Installation └── setup.md ├── 3.Basics ├── README.md ├── constants.go ├── control_structures.go ├── data_types.go ├── hello_world.go └── variables.go ├── 4.ControlStructures ├── if_else.go ├── loops.go ├── nested_loops.go └── switch.go ├── 5.Functions ├── closure.go ├── function_types.go ├── simple_function.go └── variadic_function.go ├── 6.Concurrency ├── buffered_channels.go ├── channels.go ├── deadlock_example.go ├── fan_out_fan_in.go ├── goroutines.go ├── rate_limiter.go ├── select_statement.go └── worker_pool.go ├── 7.AdvancedTopics ├── defer.go ├── embed_structs.go ├── error_handling.go ├── interfaces.go ├── intreface_composition.go ├── panic_recover.go ├── structs.go └── type_assertion.go ├── README.md ├── go.mod ├── main.go ├── managed_context └── metadata.json └── test_suite_analysis └── metadata.json /.gitignore: -------------------------------------------------------------------------------- 1 | # If you prefer the allow list template instead of the deny list, see community template: 2 | # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore 3 | # 4 | # Binaries for programs and plugins 5 | *.exe 6 | *.exe~ 7 | *.dll 8 | *.so 9 | *.dylib 10 | 11 | # Test binary, built with `go test -c` 12 | *.test 13 | 14 | # Output of the go coverage tool, specifically when used with LiteIDE 15 | *.out 16 | 17 | # Dependency directories (remove the comment below to include it) 18 | # vendor/ 19 | 20 | # Go workspace file 21 | go.work 22 | go.work.sum 23 | 24 | # env file 25 | .env -------------------------------------------------------------------------------- /1.Introduction/README.md: -------------------------------------------------------------------------------- 1 | # Introduction to Go 2 | 3 | Go, also known as **Golang**, is an open-source programming language developed by Google. Known for its simplicity, efficiency, and powerful standard library, Go is widely used in cloud computing, microservices, and distributed systems. 4 | 5 | ## 🌍 Why Go? 6 | Go was designed to address some of the major issues faced by modern developers: 7 | - **Simplicity**: Go has a minimalistic syntax, making it easy to read, learn, and use. 8 | - **Concurrency**: Go’s built-in support for concurrency through goroutines and channels makes it ideal for handling multiple tasks simultaneously, a crucial feature for high-performance applications. 9 | - **Performance**: Go compiles to machine code, offering fast execution and efficiency comparable to languages like C and C++. 10 | - **Robust Standard Library**: Go includes a powerful standard library, with tools for networking, HTTP servers, and more. 11 | - **Cross-Platform**: Write Go code once, and compile it for multiple operating systems. 12 | 13 | ## ✨ Key Features of Go 14 | ### 1. Statically Typed 15 | Go is statically typed, meaning types are checked at compile time. This reduces bugs and improves code safety and maintainability. 16 | 17 | ### 2. Fast Compilation 18 | Go compiles quickly, even for large codebases. This efficiency makes it a favorite for large-scale applications. 19 | 20 | ### 3. Concurrency with Goroutines 21 | Go’s lightweight goroutines make it easy to perform concurrent tasks without complicated thread management. This feature is especially useful in web servers, microservices, and data pipelines. 22 | 23 | ### 4. Built-In Tooling 24 | Go provides excellent built-in tools: 25 | - `go fmt`: Formats code for readability. 26 | - `go vet`: Analyzes code for potential issues. 27 | - `go test`: Built-in testing framework. 28 | - `go run`: Runs Go programs without needing to compile separately. 29 | 30 | ## 🌐 Real-World Applications of Go 31 | Go is used by some of the largest tech companies and popular projects: 32 | - **Kubernetes**: Orchestration platform for containerized applications. 33 | - **Docker**: Containerization platform for developing, shipping, and running applications. 34 | - **InfluxDB**: Time-series database optimized for high-write workloads. 35 | - **Prometheus**: Monitoring and alerting toolkit for cloud-native environments. 36 | 37 | ## 🔍 What You’ll Learn 38 | In this repository, you’ll go from the basics of Go to advanced topics, covering: 39 | 1. **Basic Syntax**: Get comfortable with Go’s syntax and conventions. 40 | 2. **Control Structures**: Use conditionals, loops, and switches. 41 | 3. **Functions and Methods**: Define functions, work with parameters, and explore methods. 42 | 4. **Data Structures**: Work with structs, slices, maps, and arrays. 43 | 5. **Concurrency**: Learn to write concurrent code with goroutines and channels. 44 | 6. **Modules and Packages**: Organize and manage dependencies in your Go applications. 45 | -------------------------------------------------------------------------------- /1.Introduction/introduction.go: -------------------------------------------------------------------------------- 1 | // Package declaration 2 | package main 3 | 4 | // Importing necessary package 5 | import "fmt" 6 | 7 | // main function: entry point of any Go program 8 | func main() { 9 | // Printing a message to the console 10 | fmt.Println("Hello, Go!") 11 | 12 | // Declaring and initializing variables 13 | var greeting string = "Welcome to the Go language!" 14 | var year int = 2024 15 | version := 1.18 // Short variable declaration for floats 16 | 17 | // Displaying variable values 18 | fmt.Println(greeting) 19 | fmt.Println("The current year is:", year) 20 | fmt.Printf("Using Go version: %.2f\n", version) 21 | 22 | // Basic types and operations 23 | number := 10 24 | increment := 2 25 | result := number + increment 26 | fmt.Printf("Result of %d + %d is %d\n", number, increment, result) 27 | 28 | // Demonstrating conditionals 29 | if year == 2024 { 30 | fmt.Println("It's 2024!") 31 | } else { 32 | fmt.Println("It's not 2024.") 33 | } 34 | 35 | // Loop example 36 | for i := 1; i <= 3; i++ { 37 | fmt.Printf("Loop iteration: %d\n", i) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /2.Installation/setup.md: -------------------------------------------------------------------------------- 1 | # Installation and Setup of Go 2 | 3 | This guide provides step-by-step instructions for installing Go on Windows, macOS, and Linux. By the end, you’ll have a working Go environment to start developing! 4 | 5 | --- 6 | 7 | ## 📥 1. Download Go 8 | 9 | Go to the official [Go download page](https://golang.org/dl/) and download the installer for your operating system. 10 | 11 | --- 12 | 13 | ## 💻 2. Installation Instructions 14 | 15 | ### Windows 16 | 17 | 1. Run the downloaded `.msi` installer and follow the installation prompts. 18 | 2. By default, Go will install to `C:\Program Files\Go`. 19 | 3. After installation, add Go’s `bin` directory to your PATH: 20 | - Open the **Start Menu** and search for **Environment Variables**. 21 | - Under **System Variables**, find and select the **Path** variable, then click **Edit**. 22 | - Add `C:\Program Files\Go\bin` to the list. 23 | 24 | ### macOS 25 | 26 | 1. Open a terminal. 27 | 2. Install Go using Homebrew: 28 | ```bash 29 | brew install go 30 | ``` 31 | 3. Alternatively, run the downloaded `.pkg` installer and follow the prompts. Go will install to `/usr/local/go` by default. 32 | 33 | ### Linux 34 | 35 | 1. Open a terminal. 36 | 2. Download the Go tarball for Linux: 37 | ```bash 38 | wget https://golang.org/dl/go1.18.linux-amd64.tar.gz 39 | ``` 40 | 3. Extract the archive to `/usr/local`: 41 | ```bash 42 | sudo tar -C /usr/local -xzf go1.18.linux-amd64.tar.gz 43 | ``` 44 | 4. Add Go’s `bin` directory to your PATH by adding this line to your shell profile (`~/.bashrc`, `~/.zshrc`, etc.): 45 | ```bash 46 | export PATH=$PATH:/usr/local/go/bin 47 | ``` 48 | 5. Refresh your profile: 49 | ```bash 50 | source ~/.bashrc # or ~/.zshrc for Zsh users 51 | ``` 52 | 53 | --- 54 | 55 | ## 🛠 3. Verify Your Installation 56 | 57 | 1. Open a terminal or command prompt. 58 | 2. Type the following command to verify your Go installation: 59 | ```bash 60 | go version 61 | ``` 62 | You should see output like `go version go1.18 `, confirming that Go is installed. 63 | 64 | --- 65 | 66 | ## ⚙️ 4. Set Up Your Go Workspace 67 | 68 | Go’s workspace is where all Go code resides. Setting up a workspace helps manage projects efficiently. 69 | 70 | 1. **Create a Workspace Folder** (e.g., `~/go`): 71 | ```bash 72 | mkdir ~/go 73 | ``` 74 | 2. Set the `GOPATH` environment variable to point to this folder by adding the following to your shell profile: 75 | ```bash 76 | export GOPATH=$HOME/go 77 | ``` 78 | 3. Confirm that your workspace is set up by creating a basic Go project. 79 | 80 | --- 81 | 82 | ## 🧪 5. Test Your Setup 83 | 84 | Create a simple “Hello, World!” program to confirm your setup: 85 | 86 | 1. Inside your workspace folder, create a new file: 87 | ```bash 88 | mkdir -p $GOPATH/src/hello 89 | cd $GOPATH/src/hello 90 | touch hello.go 91 | ``` 92 | 93 | 2. Add the following code to `hello.go`: 94 | 95 | ```go 96 | package main 97 | 98 | import "fmt" 99 | 100 | func main() { 101 | fmt.Println("Hello, World!") 102 | } 103 | ``` 104 | 105 | 3. Run the program: 106 | ```bash 107 | go run hello.go 108 | ``` 109 | 110 | If everything is set up correctly, you should see: 111 | ``` 112 | Hello, World! 113 | ``` 114 | 115 | --- 116 | 117 | Congratulations! You’re now ready to start coding with Go. For more information, see the [Go Documentation](https://golang.org/doc/). -------------------------------------------------------------------------------- /3.Basics/README.md: -------------------------------------------------------------------------------- 1 | # Basics of Go Programming 2 | 3 | Welcome to the **Basics** section! This module covers the foundational elements of Go, from simple syntax to essential control structures, types, and variables. Mastering these basics is key to becoming proficient in Go and understanding the core principles that make it unique. 4 | 5 | --- 6 | 7 | ## 📖 Topics Covered 8 | 9 | 1. **Hello, World!**: Your first Go program. 10 | 2. **Variables and Constants**: Declaring and initializing variables. 11 | 3. **Data Types**: Understanding basic and composite types in Go. 12 | 4. **Control Structures**: Using `if`, `for`, `switch`, and more. 13 | 5. **Functions**: Declaring and calling functions. 14 | 6. **Error Handling**: Introducing basic error handling. 15 | 16 | --- 17 | 18 | ## 🏗 Structure of Go Code 19 | 20 | A simple Go program begins with a **package** declaration, followed by imports, and a `main` function that serves as the entry point of the program. 21 | 22 | ```go 23 | package main // Declare the main package for executables 24 | 25 | import "fmt" // Import the fmt package for formatted I/O 26 | 27 | // Main function, the program entry point 28 | func main() { 29 | fmt.Println("Hello, World!") 30 | } 31 | ``` 32 | 33 | ### Running the Program 34 | To execute the code, save it in a file (e.g., `hello.go`), then run: 35 | ```bash 36 | go run hello.go 37 | ``` 38 | 39 | --- 40 | 41 | ## 🔢 Variables and Constants 42 | 43 | ### Declaring Variables 44 | Variables in Go can be declared in several ways: 45 | 46 | - **Explicit declaration with type**: 47 | ```go 48 | var name string = "Alice" 49 | ``` 50 | - **Type inference**: 51 | ```go 52 | age := 30 53 | ``` 54 | 55 | ### Declaring Constants 56 | Constants are immutable and declared with the `const` keyword: 57 | ```go 58 | const Pi = 3.14 59 | ``` 60 | 61 | ### Built-in Data Types 62 | Go includes several fundamental data types: 63 | - **Basic Types**: `int`, `float64`, `string`, `bool` 64 | - **Composite Types**: `array`, `slice`, `map`, `struct` 65 | 66 | ### Type Conversion 67 | Type conversions are explicit: 68 | ```go 69 | var x int = 42 70 | var y float64 = float64(x) 71 | ``` 72 | 73 | --- 74 | 75 | ## 🔄 Control Structures 76 | 77 | ### 1. **`if` and `else` Statements** 78 | ```go 79 | if x > 10 { 80 | fmt.Println("x is greater than 10") 81 | } else { 82 | fmt.Println("x is 10 or less") 83 | } 84 | ``` 85 | 86 | ### 2. **Loops (`for`)** 87 | Go has a single looping construct, `for`: 88 | ```go 89 | for i := 0; i < 5; i++ { 90 | fmt.Println(i) 91 | } 92 | ``` 93 | 94 | ### 3. **Switch Statement** 95 | ```go 96 | switch day := "Monday"; day { 97 | case "Monday": 98 | fmt.Println("Start of the work week") 99 | case "Friday": 100 | fmt.Println("Almost weekend!") 101 | default: 102 | fmt.Println("It's just a regular day") 103 | } 104 | ``` 105 | 106 | ### 4. **Defer, Panic, and Recover** 107 | - **`defer`**: Used to delay the execution of a function until the surrounding function returns. 108 | - **`panic`**: Terminates the program; used for unrecoverable errors. 109 | - **`recover`**: Catches panics for graceful handling. 110 | 111 | --- 112 | 113 | ## 📦 Composite Types 114 | 115 | 1. **Arrays and Slices**: 116 | ```go 117 | var arr [3]int = [3]int{1, 2, 3} 118 | slice := []int{4, 5, 6} // A dynamic array 119 | ``` 120 | 121 | 2. **Maps**: 122 | ```go 123 | colors := map[string]string{"red": "#FF0000", "green": "#00FF00"} 124 | ``` 125 | 126 | 3. **Structs**: 127 | Custom data types can be defined using `structs`: 128 | ```go 129 | type Person struct { 130 | Name string 131 | Age int 132 | } 133 | ``` 134 | 135 | --- 136 | 137 | ## ✨ Functions 138 | 139 | ### Defining and Calling Functions 140 | Functions are defined with the `func` keyword: 141 | ```go 142 | func add(a int, b int) int { 143 | return a + b 144 | } 145 | ``` 146 | You can call this function with: 147 | ```go 148 | sum := add(3, 4) 149 | ``` 150 | 151 | ### Returning Multiple Values 152 | Go supports multiple return values: 153 | ```go 154 | func divide(a, b int) (int, error) { 155 | if b == 0 { 156 | return 0, fmt.Errorf("cannot divide by zero") 157 | } 158 | return a / b, nil 159 | } 160 | ``` 161 | 162 | --- 163 | 164 | ## 🔍 Error Handling 165 | 166 | Go doesn’t have traditional exceptions; instead, functions often return an `error` as the second return value: 167 | ```go 168 | result, err := divide(10, 2) 169 | if err != nil { 170 | fmt.Println("Error:", err) 171 | } else { 172 | fmt.Println("Result:", result) 173 | } 174 | ``` 175 | 176 | --- 177 | 178 | ## 🚀 Practice 179 | 180 | Each file in this section demonstrates a specific Go concept. Run each example, and try modifying the code to see how Go responds. This hands-on approach will deepen your understanding and prepare you for more advanced topics! 181 | 182 | For more details on each concept, see the official [Go documentation](https://golang.org/doc/). 183 | -------------------------------------------------------------------------------- /3.Basics/constants.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | const Pi = 3.14 7 | const greeting = "Hello, Go!" 8 | 9 | fmt.Printf("Value of Pi: %.2f\n", Pi) 10 | fmt.Println(greeting) 11 | } 12 | -------------------------------------------------------------------------------- /3.Basics/control_structures.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | number := 10 7 | 8 | // If-Else 9 | if number%2 == 0 { 10 | fmt.Println("Even number") 11 | } else { 12 | fmt.Println("Odd number") 13 | } 14 | 15 | // Switch 16 | switch number { 17 | case 1: 18 | fmt.Println("One") 19 | case 2: 20 | fmt.Println("Two") 21 | case 10: 22 | fmt.Println("Ten") 23 | default: 24 | fmt.Println("Other number") 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /3.Basics/data_types.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var i int = 42 7 | var f float64 = 3.14 8 | var b bool = true 9 | var s string = "Hello, Go!" 10 | 11 | fmt.Println("Integer:", i) 12 | fmt.Println("Float:", f) 13 | fmt.Println("Boolean:", b) 14 | fmt.Println("String:", s) 15 | } 16 | -------------------------------------------------------------------------------- /3.Basics/hello_world.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("Hello, World!") 7 | } -------------------------------------------------------------------------------- /3.Basics/variables.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var name string = "Jawher" 7 | var age int = 28 8 | var height float64 = 1.79 9 | var isEngineer bool = true 10 | 11 | fmt.Printf("Name: %s\nAge: %d\nHeight: %.2f meters\nIs isEngineer: %t\n", name, age, height, isEngineer) 12 | } 13 | -------------------------------------------------------------------------------- /4.ControlStructures/if_else.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | temperature := 30 7 | 8 | if temperature > 25 { 9 | fmt.Println("It's a hot day.") 10 | } else if temperature < 15 { 11 | fmt.Println("It's a cold day.") 12 | } else { 13 | fmt.Println("It's a pleasant day.") 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /4.ControlStructures/loops.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // Standard for loop 7 | for i := 0; i < 5; i++ { 8 | fmt.Println("Iteration:", i) 9 | } 10 | 11 | // While-like loop 12 | count := 0 13 | for count < 3 { 14 | fmt.Println("Count is:", count) 15 | count++ 16 | } 17 | 18 | // Range loop 19 | fruits := []string{"Apple", "Banana", "Cherry"} 20 | for index, fruit := range fruits { 21 | fmt.Printf("Fruit at index %d is %s\n", index, fruit) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /4.ControlStructures/nested_loops.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | for i := 1; i <= 3; i++ { 7 | for j := 1; j <= 2; j++ { 8 | fmt.Printf("Outer Loop: %d, Inner Loop: %d\n", i, j) 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /4.ControlStructures/switch.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | day := "Wednesday" 7 | 8 | switch day { 9 | case "Monday": 10 | fmt.Println("It's Monday!") 11 | case "Tuesday": 12 | fmt.Println("It's Tuesday!") 13 | case "Wednesday": 14 | fmt.Println("It's Wednesday!") 15 | case "Thursday": 16 | fmt.Println("It's Thursday!") 17 | case "Friday": 18 | fmt.Println("It's Friday!") 19 | default: 20 | fmt.Println("It's the weekend!") 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /5.Functions/closure.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | // Function that returns a closure 6 | func createCounter() func() int { 7 | count := 0 8 | return func() int { 9 | count++ 10 | return count 11 | } 12 | } 13 | 14 | func main() { 15 | counter := createCounter() 16 | 17 | fmt.Println("Counter:", counter()) 18 | fmt.Println("Counter:", counter()) 19 | fmt.Println("Counter:", counter()) 20 | } 21 | -------------------------------------------------------------------------------- /5.Functions/function_types.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | // Define a function type 6 | type operation func(int, int) int 7 | 8 | // Function that performs addition 9 | func add(a int, b int) int { 10 | return a + b 11 | } 12 | 13 | // Function that performs multiplication 14 | func multiply(a int, b int) int { 15 | return a * b 16 | } 17 | 18 | func calculate(op operation, a int, b int) int { 19 | return op(a, b) 20 | } 21 | 22 | func main() { 23 | sum := calculate(add, 3, 4) 24 | product := calculate(multiply, 3, 4) 25 | 26 | fmt.Println("Sum:", sum) 27 | fmt.Println("Product:", product) 28 | } 29 | -------------------------------------------------------------------------------- /5.Functions/simple_function.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | // A simple function that adds two integers 6 | func add(a int, b int) int { 7 | return a + b 8 | } 9 | 10 | func main() { 11 | sum := add(3, 5) 12 | fmt.Println("Sum:", sum) 13 | } 14 | -------------------------------------------------------------------------------- /5.Functions/variadic_function.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | // A variadic function that sums multiple integers 6 | func sum(numbers ...int) int { 7 | total := 0 8 | for _, number := range numbers { 9 | total += number 10 | } 11 | return total 12 | } 13 | 14 | func main() { 15 | result := sum(1, 2, 3, 4, 5) 16 | fmt.Println("Total Sum:", result) 17 | } 18 | -------------------------------------------------------------------------------- /6.Concurrency/buffered_channels.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func producer(ch chan<- int) { 9 | for i := 0; i < 5; i++ { 10 | ch <- i 11 | fmt.Println("Produced:", i) 12 | time.Sleep(200 * time.Millisecond) 13 | } 14 | close(ch) 15 | } 16 | 17 | func consumer(ch <-chan int) { 18 | for num := range ch { 19 | fmt.Println("Consumed:", num) 20 | time.Sleep(300 * time.Millisecond) 21 | } 22 | } 23 | 24 | func main() { 25 | ch := make(chan int, 3) // Buffered channel with capacity of 3 26 | 27 | go producer(ch) // Start producer goroutine 28 | consumer(ch) // Start consumer 29 | 30 | fmt.Println("Main function finished.") 31 | } 32 | -------------------------------------------------------------------------------- /6.Concurrency/channels.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func producer(ch chan<- string) { 9 | for i := 1; i <= 5; i++ { 10 | msg := fmt.Sprintf("Message %d", i) 11 | ch <- msg 12 | time.Sleep(500 * time.Millisecond) 13 | } 14 | close(ch) // Close channel after sending all messages 15 | } 16 | 17 | func consumer(ch <-chan string) { 18 | for msg := range ch { 19 | fmt.Println("Received:", msg) 20 | } 21 | } 22 | 23 | func main() { 24 | ch := make(chan string) 25 | 26 | go producer(ch) // Start producer goroutine 27 | consumer(ch) // Start consumer 28 | 29 | fmt.Println("Main function finished.") 30 | } 31 | -------------------------------------------------------------------------------- /6.Concurrency/deadlock_example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | ) 7 | 8 | func main() { 9 | var wg sync.WaitGroup 10 | ch1 := make(chan struct{}) 11 | ch2 := make(chan struct{}) 12 | 13 | wg.Add(2) 14 | 15 | // Goroutine 1 16 | go func() { 17 | defer wg.Done() 18 | <-ch1 // Wait for signal 19 | fmt.Println("Goroutine 1 finished.") 20 | }() 21 | 22 | // Goroutine 2 23 | go func() { 24 | defer wg.Done() 25 | <-ch2 // Wait for signal 26 | fmt.Println("Goroutine 2 finished.") 27 | }() 28 | 29 | // Uncomment to create deadlock 30 | // ch1 <- struct{}{} 31 | // ch2 <- struct{}{} 32 | 33 | // Resolve deadlock by signaling 34 | ch1 <- struct{}{} 35 | ch2 <- struct{}{} 36 | 37 | wg.Wait() 38 | fmt.Println("Both goroutines completed successfully.") 39 | } 40 | -------------------------------------------------------------------------------- /6.Concurrency/fan_out_fan_in.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "sync" 7 | "time" 8 | ) 9 | 10 | func worker(id int, jobs <-chan int, results chan<- int, wg *sync.WaitGroup) { 11 | defer wg.Done() 12 | for job := range jobs { 13 | time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond) // Simulate processing time 14 | results <- job * 2 // Process job (e.g., double it) 15 | fmt.Printf("Worker %d processed job %d\n", id, job) 16 | } 17 | } 18 | 19 | func main() { 20 | jobs := make(chan int, 10) 21 | results := make(chan int, 10) 22 | var wg sync.WaitGroup 23 | 24 | // Start workers 25 | for w := 1; w <= 3; w++ { 26 | wg.Add(1) 27 | go worker(w, jobs, results, &wg) 28 | } 29 | 30 | // Send jobs 31 | for j := 1; j <= 5; j++ { 32 | jobs <- j 33 | } 34 | close(jobs) 35 | 36 | // Close results channel once all workers are done 37 | go func() { 38 | wg.Wait() 39 | close(results) 40 | }() 41 | 42 | // Read results 43 | for result := range results { 44 | fmt.Println("Result:", result) 45 | } 46 | fmt.Println("All jobs processed.") 47 | } 48 | -------------------------------------------------------------------------------- /6.Concurrency/goroutines.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func sayHello() { 9 | fmt.Println("Hello from Goroutine!") 10 | } 11 | 12 | func main() { 13 | go sayHello() // Start goroutine 14 | 15 | // Wait for the goroutine to finish 16 | time.Sleep(1 * time.Second) 17 | fmt.Println("Main function finished.") 18 | } 19 | -------------------------------------------------------------------------------- /6.Concurrency/rate_limiter.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func main() { 9 | rateLimit := time.Tick(1 * time.Second) // One request per second 10 | 11 | for i := 1; i <= 5; i++ { 12 | <-rateLimit 13 | fmt.Printf("Request %d sent at %v\n", i, time.Now()) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /6.Concurrency/select_statement.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func generateMessage(ch chan<- string, delay time.Duration) { 9 | time.Sleep(delay) 10 | ch <- "Message after " + delay.String() 11 | } 12 | 13 | func main() { 14 | ch1 := make(chan string) 15 | ch2 := make(chan string) 16 | 17 | go generateMessage(ch1, 2*time.Second) 18 | go generateMessage(ch2, 1*time.Second) 19 | 20 | for i := 0; i < 2; i++ { 21 | select { 22 | case msg1 := <-ch1: 23 | fmt.Println("Received:", msg1) 24 | case msg2 := <-ch2: 25 | fmt.Println("Received:", msg2) 26 | } 27 | } 28 | 29 | fmt.Println("Main function finished.") 30 | } 31 | -------------------------------------------------------------------------------- /6.Concurrency/worker_pool.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | "time" 7 | ) 8 | 9 | func worker(id int, jobs <-chan int, wg *sync.WaitGroup) { 10 | defer wg.Done() 11 | for job := range jobs { 12 | fmt.Printf("Worker %d started job %d\n", id, job) 13 | time.Sleep(time.Second) // Simulate work 14 | fmt.Printf("Worker %d finished job %d\n", id, job) 15 | } 16 | } 17 | 18 | func main() { 19 | const numWorkers = 3 20 | jobs := make(chan int, 10) 21 | var wg sync.WaitGroup 22 | 23 | // Start workers 24 | for w := 1; w <= numWorkers; w++ { 25 | wg.Add(1) 26 | go worker(w, jobs, &wg) 27 | } 28 | 29 | // Send jobs 30 | for j := 1; j <= 5; j++ { 31 | jobs <- j 32 | } 33 | close(jobs) 34 | 35 | // Wait for all workers to finish 36 | wg.Wait() 37 | fmt.Println("All jobs completed.") 38 | } 39 | -------------------------------------------------------------------------------- /7.AdvancedTopics/defer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | defer fmt.Println("World!") 7 | fmt.Println("Hello") 8 | } 9 | -------------------------------------------------------------------------------- /7.AdvancedTopics/embed_structs.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Person struct { 6 | Name string 7 | Age int 8 | } 9 | 10 | type Employee struct { 11 | Person // Embed Person 12 | Company string 13 | } 14 | 15 | func main() { 16 | emp := Employee{ 17 | Person: Person{Name: "John", Age: 30}, 18 | Company: "TechCorp", 19 | } 20 | fmt.Printf("Name: %s, Age: %d, Company: %s\n", emp.Name, emp.Age, emp.Company) 21 | } 22 | -------------------------------------------------------------------------------- /7.AdvancedTopics/error_handling.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | ) 7 | 8 | // Custom error 9 | var ErrNegativeNumber = errors.New("negative number not allowed") 10 | 11 | func squareRoot(x float64) (float64, error) { 12 | if x < 0 { 13 | return 0, ErrNegativeNumber 14 | } 15 | return x * x, nil 16 | } 17 | 18 | func main() { 19 | result, err := squareRoot(-4) 20 | if err != nil { 21 | fmt.Println("Error:", err) 22 | } else { 23 | fmt.Println("Square Root:", result) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /7.AdvancedTopics/interfaces.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | // Define an interface 6 | type Animal interface { 7 | Speak() string 8 | } 9 | 10 | // Dog implements the Animal interface 11 | type Dog struct{} 12 | 13 | func (d Dog) Speak() string { 14 | return "Woof!" 15 | } 16 | 17 | // Cat implements the Animal interface 18 | type Cat struct{} 19 | 20 | func (c Cat) Speak() string { 21 | return "Meow!" 22 | } 23 | 24 | func main() { 25 | var a Animal 26 | 27 | a = Dog{} 28 | fmt.Println("Dog:", a.Speak()) 29 | 30 | a = Cat{} 31 | fmt.Println("Cat:", a.Speak()) 32 | } 33 | -------------------------------------------------------------------------------- /7.AdvancedTopics/intreface_composition.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Reader interface { 6 | Read() string 7 | } 8 | 9 | type Writer interface { 10 | Write(string) 11 | } 12 | 13 | type IO interface { 14 | Reader 15 | Writer 16 | } 17 | 18 | type MyIO struct{} 19 | 20 | func (m MyIO) Read() string { 21 | return "Reading data" 22 | } 23 | 24 | func (m MyIO) Write(data string) { 25 | fmt.Println("Writing:", data) 26 | } 27 | 28 | func main() { 29 | var io IO = MyIO{} 30 | fmt.Println(io.Read()) 31 | io.Write("Hello, World!") 32 | } 33 | -------------------------------------------------------------------------------- /7.AdvancedTopics/panic_recover.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func riskyOperation() { 6 | defer recoverPanic() 7 | panic("something went wrong") 8 | } 9 | 10 | func recoverPanic() { 11 | if r := recover(); r != nil { 12 | fmt.Println("Recovered from:", r) 13 | } 14 | } 15 | 16 | func main() { 17 | riskyOperation() 18 | fmt.Println("After risky operation") 19 | } 20 | -------------------------------------------------------------------------------- /7.AdvancedTopics/structs.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | // Define a struct 6 | type Rectangle struct { 7 | width, height float64 8 | } 9 | 10 | // Method to calculate area 11 | func (r Rectangle) Area() float64 { 12 | return r.width * r.height 13 | } 14 | 15 | func main() { 16 | rect := Rectangle{width: 10, height: 5} 17 | fmt.Println("Area of rectangle:", rect.Area()) 18 | } 19 | -------------------------------------------------------------------------------- /7.AdvancedTopics/type_assertion.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func printType(i interface{}) { 6 | switch v := i.(type) { 7 | case int: 8 | fmt.Println("Integer:", v) 9 | case string: 10 | fmt.Println("String:", v) 11 | default: 12 | fmt.Println("Unknown type") 13 | } 14 | } 15 | 16 | func main() { 17 | printType(42) 18 | printType("Hello") 19 | } 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Go Language Study Repository 📘 2 | Welcome to the **Go Language Study** repository! This repository is dedicated to exploring and understanding the Go programming language, covering essential concepts, syntax, and best practices. Whether you’re a beginner looking to get started with Go or an experienced developer seeking deeper insights, this repository has you covered. 3 | 4 | --- 5 | 6 | ## 🌟 Overview 7 | This repository provides a structured guide to learning Go, from basics to advanced topics like concurrency and modules. Each topic includes explanations, example code, and practical exercises to help solidify your understanding. 8 | 9 | ### Key Sections: 10 | - **Introduction**: Overview of Go and its significance in modern software development. 11 | - **Installation and Setup**: Step-by-step guide for setting up Go on various platforms. 12 | - **Syntax and Basics**: Dive into Go’s syntax, variables, data types, and control structures. 13 | - **Advanced Topics**: Explore Go’s powerful features, including concurrency, interfaces, and error handling. 14 | 15 | --- 16 | 17 | ## 📂 Repository Structure 18 | 19 | The repository is organized into modules, with each folder focusing on a specific aspect of Go. Here’s a breakdown of the structure: 20 | ``` 21 | Go-Language-Study/ 22 | ├── README.md # Overview of the repository 23 | ├── 1.Introduction/ # Overview of Go language, history, and features 24 | │ └── README.md 25 | ├── 2.Installation/ # Guide for installing and setting up Go 26 | │ └── setup.md 27 | ├── 3.Basics/ # Core Go syntax and concepts 28 | │ ├── hello_world.go 29 | │ ├── variables.go 30 | │ └── README.md 31 | ├── 4.ControlStructures/ # Conditional logic and loops 32 | │ ├── if_else.go 33 | │ ├── switch.go 34 | │ └── loops.go 35 | ├── 5.Functions/ # Function definitions, parameters, and returns 36 | │ ├── simple_function.go 37 | │ └── variadic_function.go 38 | ├── 6.Concurrency/ # Concurrency with goroutines and channels 39 | │ ├── goroutines.go 40 | │ └── channels.go 41 | └── 7.AdvancedTopics/ # Advanced Go topics like interfaces and error handling 42 | ├── interfaces.go 43 | └── error_handling.go 44 | ``` 45 | --- 46 | 47 | ## 🛠 Installation and Setup 48 | 49 | 1. **Install Go**: 50 | - Follow the guide in [Installation/setup.md](Installation/setup.md) to install Go on your platform (Windows, macOS, Linux). 51 | 52 | 2. **Clone the Repository**: 53 | ```bash 54 | git clone https://github.com/your-username/Go-Language-Study.git 55 | cd Go-Language-Study 56 | ``` 57 | 58 | 3. **Run Examples**: 59 | Navigate to any folder with example `.go` files and use: 60 | ```bash 61 | go run .go 62 | ``` 63 | --- 64 | 65 | ## 📚 Topics Covered 66 | 67 | ### 1. Introduction 68 | - Overview of Go, its unique features, and reasons to learn it. 69 | 70 | ### 2. Basics 71 | - **Hello World**: First Go program and syntax introduction. 72 | - **Variables and Data Types**: Learn variable declarations, types, and constants. 73 | 74 | ### 3. Control Structures 75 | - **Conditionals**: `if`, `else`, `switch` statements. 76 | - **Loops**: `for` loops and their usage in Go. 77 | 78 | ### 4. Functions 79 | - **Basic Functions**: Function definitions, parameters, and return values. 80 | - **Advanced Functions**: Variadic functions and closures. 81 | 82 | ### 5. Concurrency 83 | - **Goroutines**: Lightweight threads managed by the Go runtime. 84 | - **Channels**: Safe communication between goroutines. 85 | 86 | ### 6. Advanced Topics 87 | - **Error Handling**: Idiomatic error handling in Go, including `panic` and `recover`. 88 | - **Interfaces and Structs**: Define custom types and implement interfaces. 89 | 90 | --- 91 | 92 | ## 🤖 Contribution Guidelines 93 | 94 | Feel free to contribute! if you have examples, corrections, or additional topics, please fork the repository and submit a pull request. 95 | 96 | 1. Fork the project 97 | 2. Create a new branch (`git checkout -b feature/AmazingFeature`) 98 | 3. Commit your changes (`git commit -m 'Add some AmazingFeature'`) 99 | 4. Push to the branch (`git push origin feature/AmazingFeature`) 100 | 5. Open a pull request 101 | 102 | --- 103 | 104 | ## 📝 License 105 | This project is licensed under the MIT License. see the [LICENSE](LICENSE) file for details. 106 | 107 | --- 108 | 109 | ## 📞 Contact 110 | For questions or feedback, reach out to **[JawherKl](mailto:kalleljawher4@gmail.com)** or submit an issue on GitHub. 111 | 112 | Happy coding! 🎉 113 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module example 2 | 3 | go 1.22.5 4 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | // Hello App 6 | func main() { 7 | fmt.Println("Hello, App!") 8 | 9 | // Variables and Data types 10 | // Variables 11 | var foo string 12 | var foo string = "Go is awesome!" 13 | var foo, bar string = "Go", "is awesome!" 14 | var ( 15 | foo string = "Go" 16 | bar string = "is awesome!" 17 | ) 18 | var foo = "What's my type?" 19 | 20 | // Constants 21 | const constant = "This is a constant" 22 | 23 | // Data Types 24 | // String 25 | var name string = "My name is Go" 26 | var bio string = `I am statically typed. 27 | I was designed at Google.` 28 | 29 | // Bool 30 | var value bool = false 31 | var isItTrue bool = true 32 | 33 | // Numeric types 34 | type byte = uint8 35 | type rune = int32 36 | 37 | var b byte = 'a' 38 | var r rune = '🍕' 39 | 40 | var f32 float32 = 1.7812 // IEEE-754 32-bit 41 | var f64 float64 = 3.1415 // IEEE-754 64-bit 42 | 43 | var c1 complex128 = complex(10, 1) 44 | var c2 complex64 = 12 + 4i 45 | 46 | var i int 47 | var f float64 48 | var b bool 49 | var s string 50 | 51 | fmt.Printf("%v %v %v %q\n", i, f, b, s) 52 | } -------------------------------------------------------------------------------- /managed_context/metadata.json: -------------------------------------------------------------------------------- 1 | {"current_schema_version":"0.0.1"} -------------------------------------------------------------------------------- /test_suite_analysis/metadata.json: -------------------------------------------------------------------------------- 1 | {"current_schema_version":"0.0.1"} --------------------------------------------------------------------------------