Go Language Guide for Beginners
1. Introduction to Go
Go is an open-source programming language developed by Google. It is designed for simplicity,
efficiency, and scalability.
2. Installation & Setup
Download and install Go from https://golang.org/dl/.
After installation, set up the Go workspace and verify installation with 'go version'.
3. Basic Syntax
A Go program consists of packages, functions, and statements.
Example:
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
4. Variables & Data Types
Go has basic data types like int, float64, string, and bool.
Variables can be declared using 'var' or shorthand ':='.
Example:
var x int = 10
y := "Hello"
5. Control Structures
Go supports 'if-else', 'switch', and loops ('for').
Go Language Guide for Beginners
Example:
for i := 0; i < 5; i++ {
fmt.Println(i)
}
6. Arrays, Slices & Maps
Go has fixed-size arrays, dynamic slices, and key-value maps.
Example:
arr := [3]int{1, 2, 3}
slice := []int{1, 2, 3, 4}
m := map[string]int{"a": 1, "b": 2}
7. Functions
Functions in Go can return multiple values.
Example:
func add(a int, b int) int {
return a + b
}
8. Structs & Interfaces
Go supports user-defined types with 'struct' and 'interface'.
Example:
type Person struct {
Name string
Age int
}
9. Concurrency with Goroutines
Go Language Guide for Beginners
Go uses 'goroutines' for lightweight concurrency.
Example:
go func() {
fmt.Println("Running concurrently")
}()
10. File Handling
Reading and writing files in Go is done using 'os' and 'io/ioutil' packages.
Example:
data, _ := ioutil.ReadFile("file.txt")
fmt.Println(string(data))
11. Simple Web Server
Go provides 'net/http' for web development.
Example:
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, Web!")
})
http.ListenAndServe(":8080", nil)