Go, or Golang, is an open-source programming language designed by Google. It is statically typed, compiled, and known for its simplicity, performance, and efficient concurrency handling. Go is widely used for building scalable and high-performance applications.
- Simple Syntax: Designed for readability and ease of use.
- Statically Typed: Strong type-checking at compile time.
- Garbage Collection: Automatic memory management.
- Concurrency Support: Built-in support for concurrent programming with goroutines and channels.
- Fast Compilation: Compiles quickly to native machine code.
- Standard Library: Rich library for networking, file handling, and other utilities.
-
Hello World:
package main import "fmt" func main() { fmt.Println("Hello, Go!") }
-
Comments:
// Single-line comment /* Multi-line comment */
-
Declaration with
var
or shorthand (:=
):var name string = "Go" age := 10 // shorthand
-
Constants:
const Pi = 3.14
- Basic Types:
int
,float64
,string
,bool
- Composite Types:
array
,slice
,map
,struct
- Special Types:
interface
,chan
,func
if x > 0 {
fmt.Println("Positive")
} else if x == 0 {
fmt.Println("Zero")
} else {
fmt.Println("Negative")
}
-
For Loop (the only loop in Go):
for i := 0; i < 5; i++ { fmt.Println(i) }
-
Range:
numbers := []int{1, 2, 3} for i, num := range numbers { fmt.Println(i, num) }
-
Definition:
func add(a int, b int) int { return a + b }
-
Multiple Return Values:
func divide(a, b int) (int, int) { return a / b, a % b }
-
Anonymous Functions:
func() { fmt.Println("Anonymous function!") }()
- Direct memory address manipulation:
var x int = 10 var ptr *int = &x fmt.Println(*ptr) // Dereferences the pointer
-
Structs:
type Person struct { Name string Age int } p := Person{Name: "Alice", Age: 30} fmt.Println(p.Name)
-
Methods:
func (p Person) Greet() string { return "Hello, " + p.Name }
- Define behavior for types:
type Greeter interface { Greet() string }
- Lightweight thread managed by the Go runtime:
go func() { fmt.Println("Concurrent execution") }()
- Communication mechanism between goroutines:
ch := make(chan int) go func() { ch <- 42 }() fmt.Println(<-ch)
- Go uses explicit error handling:
result, err := divide(10, 0) if err != nil { fmt.Println("Error:", err) }
-
Reading from a File:
data, err := os.ReadFile("example.txt") if err != nil { log.Fatal(err) } fmt.Println(string(data))
-
Writing to a File:
err := os.WriteFile("example.txt", []byte("Hello, Go!"), 0644) if err != nil { log.Fatal(err) }
-
Organize code into packages:
package mathutils func Add(a, b int) int { return a + b }
-
Importing packages:
import "mathutils" result := mathutils.Add(3, 5)
- Use clear and concise variable names.
- Leverage Go’s strong typing for safety.
- Prefer idiomatic Go patterns (e.g., error handling).
- Optimize for simplicity and readability.
- Use
go fmt
to format code consistently.
- Official Documentation: https://go.dev/doc/
- Standard Library Reference: https://pkg.go.dev/std
- Effective Go: https://go.dev/doc/effective_go
Go’s simplicity, performance, and excellent concurrency support make it an ideal choice for modern, high-performance applications.