Go, also known as Golang, is a popular programming language that has gained a lot of popularity in recent years. It is an open-source language that is designed to be simple, efficient, and easy to use. However, like any other programming language, it can be challenging to remember all the syntax and functions. That’s where a Golang cheat sheet comes in handy.
A Golang cheat sheet is a quick reference guide that contains all the essential syntax, functions, and commands of the language. It is a handy tool for both beginners and experienced programmers who want to quickly look up a specific command or syntax. The cheat sheet is designed to be easy to read and understand, with clear examples and explanations.
The Golang cheat sheet covers all the essential topics, including variables, data types, control structures, functions, arrays, slices, maps, pointers, and more. It also includes examples of how to use each command and syntax, making it easy to understand and apply in your code.
This cheat sheet provides a quick reference for some of the most commonly used features of the Go programming language.
Declare and initialize a variable x with value 5 (short variable declaration)
const x = 5
Declare a constant x with value 5
const ( x = 5 y = 10 )
Declare multiple constants x and y with values 5 and 10 respectively
Data Types
Type
Description
bool
Boolean type (true or false)
int
Integer type (32 or 64 bits depending on the platform)
float32
Floating point type (32 bits)
float64
Floating point type (64 bits)
string
String type
byte
Alias for uint8
rune
Alias for int32
complex64
Complex number type with float32 real and imaginary parts
complex128
Complex number type with float64 real and imaginary parts
Control Structures
If Statements
Syntax
Description
if x > 5 { ... }
If x is greater than 5, execute the code inside the curly braces
if x > 5 { ... } else { ... }
If x is greater than 5, execute the code inside the first set of curly braces, otherwise execute the code inside the second set of curly braces
if x := 5; x > 5 { ... } else { ... }
Declare and initialize x with value 5, then if x is greater than 5, execute the code inside the first set of curly braces, otherwise execute the code inside the second set of curly braces
Switch Statements
Syntax
Description
switch x { case 1: ... case 2: ... default: ... }
Evaluate x and execute the code associated with the first matching case (or the default case if no cases match)
switch { case x > 5: ... case x < 5: ... default: ... }
Evaluate each case expression in order and execute the code associated with the first matching case (or the default case if no cases match)
Loops
Syntax
Description
for i := 0; i < 10; i++ { ... }
Execute the code inside the curly braces 10 times, with i ranging from 0 to 9
for { ... }
Execute the code inside the curly braces indefinitely
for i, v := range arr { ... }
Iterate over the elements of arr, executing the code inside the curly braces for each element and setting i to the index and v to the value of the element
Functions
Syntax
Description
func f() { ... }
Declare a function f with no parameters and no return value
func f(x int) { ... }
Declare a function f with one parameter x of type int and no return value
func f() int { ... }
Declare a function f with no parameters and a return value of type int
func f(x int) int { ... }
Declare a function f with one parameter x of type int and a return value of type int
func f(x, y int) (int, int) { ... }
Declare a function f with two parameters x and y of type int and two return values of type int
Pointers
Syntax
Description
var x int var p *int = &x
Declare a variable x of type int and a pointer p to x
*p = 5
Set the value of x to 5 through the pointer p
var p *int if p != nil { ... }
Declare a pointer p to int and check if it is nil
var p *int p = new(int)
Declare a pointer p to int and allocate memory for an int value
Arrays and Slices
Arrays
Syntax
Description
var arr [5]int
Declare an array arr of length 5 and type int
arr := [5]int{1, 2, 3, 4, 5}
Declare and initialize an array arr of length 5 and type int with values 1, 2, 3, 4, and 5
arr[0] = 1
Set the first element of arr to 1
len(arr)
Get the length of arr
Slices
Syntax
Description
var s []int
Declare a slice s of type int
s := []int{1, 2, 3, 4, 5}
Declare and initialize a slice s of type int with values 1, 2, 3, 4, and 5
s = append(s, 6)
Append the value 6 to the end of s
s = s[1:4]
Create a new slice s that includes elements 1 through 3 of the original slice
len(s)
Get the length of s
Maps
Syntax
Description
var m map[string]int
Declare a map m with keys of type string and values of type int
m := map[string]int{""one"": 1, ""two"": 2}
Declare and initialize a map m with keys ""one"" and ""two"" and values 1 and 2 respectively
m[""three""] = 3
Add a key-value pair ""three"": 3 to m
delete(m, ""two"")
Remove the key-value pair with key ""two"" from m
len(m)
Get the number of key-value pairs in m
Structs
Syntax
Description
type Person struct { name string age int } var p Person
Declare a struct type Person with fields name and age, and declare a variable p of type Person
p := Person{name: ""Alice"", age: 30}
Declare and initialize a variable p of type Person with fields name set to ""Alice"" and age set to 30
p.name = ""Bob""
Set the name field of p to ""Bob""
p.age
Get the value of the age field of p
Interfaces
Syntax
Description
type Shape interface { area() float64 }
Declare an interface Shape with one method area that returns a float64
type Circle struct { x, y, r float64 } func (c Circle) area() float64 { ... }
Declare a struct type Circle with fields x, y, and r, and implement the area method for Circle
var s Shape s = Circle{x: 0, y: 0, r: 5} s.area()
Declare a variable s of type Shape and assign a Circle value to it, then call the area method on s
Concurrency
Syntax
Description
go f()
Execute the function f in a new goroutine
ch := make(chan int)
Declare a channel ch of type int
ch <- x
Send the value x on the channel ch
x := <-ch
Receive a value from the channel ch and assign it to x
select { case x := <-ch1: ... case x := <-ch2: ... }
Wait for a value to be received on either ch1 or ch2, and execute the code associated with the first case that is ready