Go Tutorial
Go (or Golang) is a modern programming language developed by Google, designed for building fast and reliable applications, especially in cloud, DevOps, and distributed systems. Nowadays, many big tech companies have also adopted and rely on it, including:
- Google uses for services behind YouTube and Google Cloud.
- Uber moved parts of their real-time ride systems to Go for speed.
- Netflix uses it for server-side services that need quick responses.
- Dropbox rewrote key infrastructure in Go to handle millions of file syncs per second, etc.
First Go Program
Here is a simple Go program that prints a string. You can try editing it to print your own name.
package main
import "fmt"
// Main function
func main() {
fmt.Println("Hello Geeks")
}
Output:
Hello GeeksGetting Started with Go
Before starting with Go programming, we first need to set up Go on our system and run a simple program to verify the installation:
Fundamentals of Go
In this section, we’ll cover the core building blocks of Go such as identifiers, variables, data types, operators, and declarations that form the foundation of every Go program.
- Identifiers in Golang
- Keywords in Golang
- Data Types
- Variables
- Constants
- Rune in Golang
- Operators in Golang
- Scope of Variables
- Type Casting
- var Keyword in Golang
- Short Declaration Operator(:=)
- var keyword vs short declaration operator
Control Statements
This section covers decision-making and looping constructs in Go, essential for controlling program flow.
- Decision Making Statements
- Loops in Golang
- Loop Control Statements
- Switch Statement in Go
- Deadlock and Default Case in Select Statement
Functions & Methods
In this section, we’ll explore Go functions and methods, including how to define them, pass arguments, return multiple values, use special features like defer, and work with methods for struct types.
- What are the Functions?
- Variadic Function
- Anonymous Function
- main and init function
- Function Arguments
- Function Returning Multiple Values
- Named Return Values
- Blank Identifier
- Defer
- Methods
- Methods With Same Name
Structures in Go
In this section, we’ll explore Go structures, how to define and nest them, and how methods and fields can be promoted or even used as function fields for flexible data handling.
- Structures
- Structure Equality
- Nested Structure
- Anonymous Structure and Fields
- Promoted Fields in Structure
- Promoted Methods in Structure
- Function as a Field in Structure
Arrays & Slices
In this section, we’ll learn how Go handles collections using arrays and slices, covering their creation, manipulation, comparison, and common operations like sorting, trimming, and splitting.
- Arrays in Golang
- Copying an Array into Another Array
- Passing an Array to a Function
- Slices in Golang
- Slice Composite Literal
- Copying one Slice into another Slice
- Passing a Slice to Function
- Comparing two Slices
- Checking the Equality of Slices
- Sorting a Slice
- Trimming a Slice
- Splitting a Slice
String
In this section, we’ll work with strings in Go, learning how to compare, concatenate, trim, split, search, and repeat them, along with common operations like finding indexes and counting characters.
- Strings in Go
- Different ways to compare Strings
- Different ways to concatenate two strings
- Trimming a String in Go
- Splitting a String in Go
- Check if the given characters are present in String
- Repeating a String for Specific Number of Times
- Finding the index value of specified string
- Counting the Number of Repeated Characters in String
Pointers
In this section, we’ll learn about pointers in Go, how they reference memory addresses, and how to use them with functions, arrays, and structures effectively.
- Pointers
- Pointer to Pointer (Double Pointer)
- Pointers to a Function
- Returning Pointer from a Function
- Pointer to an Array as Function Argument
- Pointer to Struct
- Comparing Pointers
- Finding the Capacity of the Pointer
- Finding the Length of the Pointer
Interfaces
In this section, we’ll learn about interfaces in Go, how they enable polymorphism, the use of multiple and embedded interfaces, and how they help in writing flexible and reusable code.
Concurrency
In this section, we’ll learn Go’s powerful concurrency model using goroutines and channels, and see how Go handles multitasking efficiently compared to traditional threads.
- Goroutines – Concurrency
- Select Statement
- Multiple Goroutines
- Goroutine vs Thread
- Channel in Golang
- Unidirectional Channel in Golang