Go Tutorial
Last Updated : 03 Sep, 2025
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() {
[Link]("Hello Geeks")
Output:
Hello Geeks
Getting 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:
Installing Golang on Windows / Installing Golang on MacOS
Introduction to Golang
Hello World! in Golang
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.
Interfaces
Multiple Interfaces
Embedding Interfaces
Polymorphism Using Interfaces
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
main and init function in Golang
Last Updated : 22 Aug, 2024
The Go language reserve two functions for special purpose and the functions
are main() and init() function.
main() function
In Go language, the main package is a special package which is used with the programs that are
executable and this package contains main() function. The main() function is a special type of
function and it is the entry point of the executable programs. It does not take any argument nor
return anything. Go automatically call main() function, so there is no need to call main() function
explicitly and every executable program must contain single main package and main() function.
Example:
// Go program to illustrate the
// concept of main() function
// Declaration of the main package
package main
// Importing packages
import (
"fmt"
"sort"
"strings"
"time"
// Main function
func main() {
// Sorting the given slice
s := []int{345, 78, 123, 10, 76, 2, 567, 5}
[Link](s)
[Link]("Sorted slice: ", s)
// Finding the index
[Link]("Index value: ", [Link]("GeeksforGeeks", "ks"))
// Finding the time
[Link]("Time: ", [Link]().Unix())
Output:
Sorted slice: [2 5 10 76 78 123 345 567]
Index value: 3
Time: 1257894000
init() Function
init() function is just like the main function, does not take any argument nor return anything. This
function is present in every package and this function is called when the package is initialized. This
function is declared implicitly, so you cannot reference it from anywhere and you are allowed to
create multiple init() function in the same program and they execute in the order they are created.
You are allowed to create init() function anywhere in the program and they are called in lexical file
name order (Alphabetical Order). And allowed to put statements if the init() function, but always
remember to init() function is executed before the main() function call, so it does not depend
to main() function. The main purpose of the init() function is to initialize the global variables that
cannot be initialized in the global context.
Example:
// Go program to illustrate the
// concept of init() function
// Declaration of the main package
package main
// Importing package
import "fmt"
// Multiple init() function
func init() {
[Link]("Welcome to init() function")
func init() {
[Link]("Hello! init() function")
// Main function
func main() {
[Link]("Welcome to main() function")
Output:
Welcome to init() function
Hello! init() function
Welcome to main() function