Go, commonly referred to as "Golang," is an open-source programming language developed by Google in 2007. It's designed with simplicity and efficiency in mind, making it a great choice for building scalable and reliable software. Go's design focuses on productivity for developers, offering features that help write clean and efficient code.
-
Concurrency Support:
Go has built-in support for concurrency using lightweight threads called "goroutines." Goroutines enable efficient concurrent execution without the complexity of traditional threading. -
Garbage Collection:
Go has an automatic garbage collection mechanism that manages memory allocation and deallocation, reducing the burden on developers. -
Fast Compilation:
Go's compiler is incredibly fast, allowing for quick iteration during development. -
Static Typing:
Go is statically typed, which means variable types are checked at compile time, helping catch errors early. -
Simplicity:
Go's syntax is minimalistic and easy to understand, making it beginner-friendly and reducing the cognitive load on developers. -
Strong Standard Library:
Go comes with a rich standard library that includes packages for various tasks, such as networking, file I/O, cryptography, and more. -
Cross-Platform:
Go supports compilation to various platforms, ensuring your code can run consistently across different systems. -
Open Source:
Go is open-source and has a strong community of developers contributing libraries, tools, and resources.
Variable Declaration:
Declare variables using the var keyword, followed by the variable name and type.
var age int
var name string
Variable Initialization:
Initialize variables with values at the time of declaration.
var count int = 10
var message string = "Hello, Go!"
Type Inference:
Go can infer variable types if values are provided.
var score = 95 // score is inferred as int
Short Declaration:
Use := for short variable declaration.
speed := 50 // Inferred as int
Operators:
- Arithmetic Operators: +, -, *, /, %.
- Comparison Operators: ==, !=, <, >, <=, >=.
- Logical Operators: &&, ||, !.
if age >= 18 {
fmt.Println("You are an adult.")
} else {
fmt.Println("You are a minor.")
}
switch Month {
case "January":
fmt.Println("It's the first month of the year.")
case "February":
fmt.Println("It's the second month of the year.")
default:
fmt.Println("It's just another month.")
}
- For Loop:
for i := 0; i < 5; i++ {
fmt.Println(i)
}
- Range Loop:
numbers := []int{1, 2, 3, 4, 5}
for index, value := range numbers {
fmt.Printf("Index: %d, Value: %d\n", index, value)
}
Given below is the traditional "Hello, World!" program in Go:
package main
import (
"fmt"
}
func main() {
fmt.Println("Hello, World!")
}
In this program:
- package main: Defines the package name. The main package is the entry point for executable programs.
- import "fmt": Imports the "fmt" package for formatted I/O.
- func main(): The main function is where program execution starts.
- fmt.Println("Hello, World!"): Prints "Hello, World!" as the output.