Go Fundamentals
Published: 2024-06-17 12:34:01

#### Basic Syntax and Structure

**1. Go Program Structure**

A Go program typically consists of multiple packages, and the main package serves as the entry point of the program. The basic structure of a Go program is:

```go
package main

import "fmt"

// main function - the entry point of the program
func main() {
    fmt.Println("Hello, World!")
}
```

- **Package Declaration**: Every Go file begins with a `package` declaration. The `main` package is special because it defines a standalone executable program.
- **Import Statements**: The `import` keyword is used to include other packages. For example, `fmt` is a package for formatted I/O operations.
- **Function Definition**: Functions are defined using the `func` keyword. The `main` function is the entry point of the program.

**2. Variables and Constants**

Variables and constants are fundamental to any programming language, allowing the storage and manipulation of data.

- **Variables**:
  - Declared using the `var` keyword.
  - Can be initialized when declared, or later.

  ```go
  var x int
  var y int = 10
  z := 20 // shorthand for declaring and initializing a variable
  ```

- **Constants**:
  - Declared using the `const` keyword.
  - Cannot be changed once initialized.

  ```go
  const Pi = 3.14
  const Greeting = "Hello, World!"
  ```

**3. Basic Data Types**

Go has several built-in data types:

- **Strings**: Represent a sequence of characters.
  ```go
  var name string = "Gopher"
  ```
- **Integers**: Represent whole numbers.
  ```go
  var age int = 25
  ```
- **Floats**: Represent floating-point numbers.
  ```go
  var height float64 = 5.9
  ```
- **Booleans**: Represent true or false values.
  ```go
  var isActive bool = true
  ```

---

#### Control Structures

**1. Conditionals**

- **If Statement**: Executes a block of code if a specified condition is true.
  ```go
  if x > 10 {
      fmt.Println("x is greater than 10")
  }
  ```

- **If-Else Statement**: Provides an alternative block of code if the condition is false.
  ```go
  if x > 10 {
      fmt.Println("x is greater than 10")
  } else {
      fmt.Println("x is 10 or less")
  }
  ```

- **If-Else If-Else Statement**: Checks multiple conditions sequentially.
  ```go
  if x > 10 {
      fmt.Println("x is greater than 10")
  } else if x == 10 {
      fmt.Println("x is exactly 10")
  } else {
      fmt.Println("x is less than 10")
  }
  ```

- **Switch Statement**: An alternative to multiple if-else if statements, providing a cleaner syntax.
  ```go
  switch day {
  case "Monday":
      fmt.Println("Start of the work week")
  case "Friday":
      fmt.Println("End of the work week")
  default:
      fmt.Println("It's a regular day")
  }
  ```

**2. Loops**

- **For Loop**: The only looping construct in Go, but it can be used in various forms.

  - Traditional For Loop:
    ```go
    for i := 0; i < 10; i++ {
        fmt.Println(i)
    }
    ```

  - While-Like Loop:
    ```go
    i := 0
    for i < 10 {
        fmt.Println(i)
        i++
    }
    ```

  - Infinite Loop:
    ```go
    for {
        fmt.Println("Infinite loop")
    }
    ```

---

#### Functions

**1. Defining and Calling Functions**

Functions in Go are defined using the `func` keyword. They can have parameters and return values.

- **Basic Function**:
  ```go
  func greet() {
      fmt.Println("Hello, World!")
  }

  func main() {
      greet()
  }
  ```

- **Function with Parameters**:
  ```go
  func add(a int, b int) int {
      return a + b
  }

  func main() {
      sum := add(5, 7)
      fmt.Println(sum)
  }
  ```

- **Function with Multiple Return Values**:
  ```go
  func divide(a int, b int) (int, int) {
      quotient := a / b
      remainder := a % b
      return quotient, remainder
  }

  func main() {
      q, r := divide(10, 3)
      fmt.Println("Quotient:", q, "Remainder:", r)
  }
  ```

**2. Anonymous Functions and Closures**

- **Anonymous Functions**: Functions without a name, often used as literals.
  ```go
  func main() {
      func() {
          fmt.Println("Anonymous function")
      }()
  }
  ```

- **Closures**: Anonymous functions that capture variables from their surrounding scope.
  ```go
  func main() {
      x := 10
      increment := func() int {
          x++
          return x
      }
      fmt.Println(increment()) // Output: 11
      fmt.Println(increment()) // Output: 12
  }
  ```

By mastering these fundamental aspects of Go, you'll be well-equipped to handle more advanced topics and build robust applications. The simplicity and clarity of Go's syntax and structures make it an excellent choice for both new and experienced developers.