Go language Cheatsheet Cheatsheet

Go language is an open-source, statically typed programming language by Google. Go is highly recommended for creation of highly scalable and available web applications.


Sample Go program

package main
import "fmt"

func main() {
	fmt.Printf("Go Hello, World!")
  • package -- package declaration which is mandatory for all Go programs.
  • import "fmt" -- used to import built-in fmt package.
  • func main() -- function is the starting point of execution of any Go program.
  • fmt.printf -- inbuilt library function which is used to print the given message.
  • fmt.Scanf -- inbuilt library function which is used to read the data.
  • // -- to comment a single line
  • /**/ -- to comment multiple lines


Declaring Variables

var variable-name data-type

Assigning value to variables

variable-name := value // It's not mandatory to declare a variable when you use this.


  var i int  // declaring int variable at functional level
  j := 99 // short assignment without var declaration

Data types

1. Numeric Data types

Integer Data types

Data typeDescriptionSizeRange
uint88-bit unsigned integer1 byte0 to 255
int88-bit signed integer1 byte-128 to 127
int1616-bit signed integer2 bytes-32768 to 32767
unit1616-bit unsigned integer2 bytes0 to 65,535
int3232-bit signed integer4 bytes-2,147,483,648 to 2,147,483,647
uint3232-bit unsigned integer4 bytes0 to 4,294,967,295
int6464-bit signed integer8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
uint6464-bit unsigned integer8 bytes0 to 18,446,744,073,709,551,615

Float Data types

Data typeDescription
float3232-bit signed floating point number
float64-bit signed floating point number
complex32Number has float32 real and imaginary parts
complex64Number has float32 real and imaginary parts

2. Boolean Data types

Data typeDescriptionSizeRange
boolStores either true or false1 byteTrue or false

3. String Data types

Data typeDescription
stringsequence of characters


var (
	integer uint32 =  1<<32 - 1
	flt float64 = 3.14
    complexNum complex128 = cmplx.Sqrt(8 - 6i)
    str string = "Hello World"
    boolean bool = true


Operator typeDescription
Arithmetic Operator+ , - , * , / , %
comparision Operator< , > , <= , >=, != , ==
Bitwise Operator& , ^ , |, &^, <<, >>
Logical Operator&& , ||, !
Assignment Operator= , += , -= , *= , /= , %=, <<=, >>=, &=, ^=, |=

String Functions

Function nameDescription
len(str)to return the length of string str
strings.Compare(a, b)Compares two strings a and b lexically and returns 0 if the both strings are equal and 1 if string 1 is greater than string 2 and -1 if string 1 is less than string 2.
strings.Contains(str, substr)returns true if substring is found in the string str
strings.ToUpper(str)to change the str to Upper Case
strings.ToLower(str)to change the str to lower Case
strings.HasPrefix(str,"prefix")returns true if the string str is starting with a prefix
strings.HasSuffix(str,"suffix")returns true if the string str is ending with a suffix
strings.Index(str, substr)searches for a particular text within a string and returns it's index. If not found then it will returns -1.
strings.Join(stringSlice []string, sep string)contanates the elements of an slice with seperators
strings.Count(str, sep string)returns the count of number of non-overlapping instances of a character/string/text in string



var array-name[size] data-type; // declaration of an array
array-name := [size] data-type {value0,value1,…,value_size-1} // assigning values to array


var fruits [3] string //Declaring a string array of size 3  
fruits[0] = "Mango"
fruits[1] = "Apple"
fruits[2] = "Orange"
arr := [...] int {1,2,3,4,5} //Declaring a integer array of size 5

Conditional Statements

1. If


2. If-else

} else {

3. If-else-if ladder

} else if(conditional-expression-2) {
} else if(conditional-expression-3) {
else {

4. Switch

case value1:    
 break;  //optional  
case value2:    
 break;  //optional  
 //code to be executed when all the above cases are not matched;    


1. For

for(Initialization; Condition; Increment/decrement){  

2. While


3. Do-While

} while(condition); 


func function_name(parameters) return_type{ // defining a function
function_name(parameters) // calling a function


var pointername *datatype;


num := 10;     
var ptr *int;   // pointer variable
ptr = &num;


type structure_name struct{ // defining a structure

   member definition;
   member definition;
   member definition;

var structure-variable structure-name //declaring a structure variable


A slice can be formed from an array using a low and high bound indices which are separated by a colon:

array-name[lowIndex : highIndex]
  • Slices never store any data
  • Slices just describes the section of an array
  • If you modify the elements present in a Slice, it's corresponding array elements also will see changes.
  • If you omit high or low bounds while slicing, Go automatically use their defaults instead. The default values for low bound is zero and for the high bound is the length of the slice.
  • Slice can be created by using built-in function make. The below creates a slice [0 0 0 0 0]


Maps is an unordered collection of key and its value.

var map-name map[key-data-type]value-data-type //declaring a map
map-name = make(map[key-data-type]value-data-type) //defining a map


Go supports concurrent execution of tasks using Goroutines and Channels.


Advantage of Go-routines is that the control will not wait for the execution of function to complete. It will just call the function and continue to execute the rest of the code. Function execution will happen in concurrent with the rest of code execution.

go function-name([arguments])


Channels are like medium where we send values from one goroutine to another.

Declaring a channel

var channel-name chan Data-Type


channel-name:= make(chan Data-Type)

©Copyright 2020 OneCompiler | Privacy Policy | Terms & Conditions