Go
Go
Go (often referred to as
**Golang**) is a statically typed, compiled language designed by Google. It's known
for its simplicity, speed, and efficiency in building scalable systems. Go is
widely used in cloud computing, web development, microservices, and networking
applications.
---
#### Installation:
1. **Download Go**: Go to the official website [golang.org](https://golang.org/dl/)
and download the latest version of Go for your operating system.
2. **Install**: Follow the installation instructions on the site.
```bash
go version
```
```bash
go env
```
---
**Code:**
```go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
```
#### Explanation:
- **`package main`**: In Go, every program starts with a package declaration. The
`main` package is the entry point for the program.
- **`import "fmt"`**: Go uses the `import` statement to include packages that
provide useful functions. `fmt` is used for formatted I/O (like printing to the
console).
- **`func main()`**: The `main` function is the entry point of a Go program (just
like `main()` in C/C++).
- **`fmt.Println()`**: Prints the text "Hello, World!" to the console, followed by
a newline.
---
Go is statically typed, meaning you must declare the type of a variable. But, Go
has a short variable declaration syntax that makes it easier to declare variables.
**Example:**
```go
package main
import "fmt"
func main() {
var age int = 25 // Integer
var name string = "Alice" // String
var height float64 = 5.7 // Float
var isStudent bool = true // Boolean
```go
age := 25 // Go infers the type
name := "Alice"
height := 5.7
isStudent := true
---
Go has familiar control flow statements like `if`, `else`, and loops (`for` loops).
```go
package main
import "fmt"
func main() {
age := 18
if age >= 18 {
fmt.Println("You are an adult.")
} else {
fmt.Println("You are a minor.")
}
}
```
**Loops:**
Go only has a `for` loop (it can act as a `while` loop, `for-each` loop, etc.).
```go
package main
import "fmt"
func main() {
// Classic for loop
for i := 0; i < 5; i++ {
fmt.Println(i)
}
---
### 5. **Functions**
Functions in Go are declared using the `func` keyword. Functions can return values
and accept parameters.
**Example:**
```go
package main
import "fmt"
func main() {
sum := add(3, 4) // Calling the function
fmt.Println("Sum:", sum)
}
```
In Go, the return type comes after the parameters. If the function has more than
one return value, the types of all return values are specified.
---
```go
package main
import "fmt"
func main() {
var arr [5]int // Array of 5 integers
arr[0] = 10
arr[1] = 20
fmt.Println(arr)
}
```
```go
package main
import "fmt"
func main() {
// Creating a slice
nums := []int{1, 2, 3, 4, 5}
---
Go doesn’t have traditional classes, but you can use structs to group related data.
You can also define methods for structs.
```go
package main
import "fmt"
// Define a struct
type Person struct {
Name string
Age int
}
func main() {
person1 := Person{Name: "Alice", Age: 30}
person1.greet() // Call the method
}
```
---
Go has built-in support for concurrent programming with goroutines and channels.
**Example:**
```go
package main
import "fmt"
import "time"
func main() {
go sayHello() // Run sayHello() as a goroutine
In the example, `go sayHello()` runs the `sayHello()` function concurrently with
the rest of the program.
---
Go encourages explicit error handling. Functions that can fail usually return an
error value, which you can check.
**Example:**
```go
package main
import (
"fmt"
"errors"
)
func main() {
result, err := divide(10, 0)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Result:", result)
}
}
```
---
Would you like to go deeper into any specific topic or need help with a particular
Go concept?