Cheat sheet for programming languages

Cheat sheet for programming languages

April 28, 2024

Basics

PHPPythonGolangTypeScript
nullNonenilnull
Undefined keyundefined
Equal
$var1 == $var2
var1 == var2
var1 == var2
var1 == var2
Strict equal
$var1 === $var2
var1 === var2
Null Coalescence
$var ?? 'default'
var ?? 'default'
Null Assertion (Ignore null value)
var!
  • Strict equal in TypeScript
    • 2 variables have the same types

Syntax for types

PHPPythonGolangTypeScript
Type Alias
type NewType OldType
type VariableType = OldType
type FunctionType = (var: number) => void
Interface
interface Interface {
  method(var int) void
}
interface VariableInterface {
  var: number;
}
interface FunctionInterface {
  (var: number): void;
}

The difference of type and interface between TypeScript:

  • The type can be used for a type alias, but not for interface
  • The class/interface is static, so it cannot extend the union of types
  • An interface can be defined multiple times
  • An interface isn’t used to rename primitives
  • An interface has a performance advantage. See this article

These were discussed in the following articles:

Syntax for control flow

PHPPythonGolangTypeScript
if, else if, else
if ($condition1) {
    // logic 1
} else if ($condition2) {
    // logic 2
} else {
    // logic 3
}
if $condition1:
    # logic 1
elif $condition2:
    # logic 2
else:
    # logic 3
if condition1 {
  // logic 1
} else if condition 2 {
  // logic 2
} else {
  // logic 3
}
for
for ($i = 0; $i < 10; $i++) {
  # do something
}
for i in range(10):
  # do something
for i := 0; i < 10; i++ {
  // do something
}
foreach for an array without a index
foreach ($array as $value) {
  // do something
}
for value in list:
  # do something
for _, value := range slice {
  // do something
}
foreach for an array with an index
foreach ($array as $index => $value) {
  // do something
}
for index, value in enumerate(list):
  # do something
for index, value := range slice {
  // do something
}
// Or
for index := range slice {
  value := slice[index]
  // do something
}
foreach for a hash
foreach ($array as $key => $value) {
  // do something
}
for key, value in dictionary.items():
  # do something
for key, value := range map {
  // do something
}

Operations for common types

Numbers

PHPPythonGolangTypeScript
Decimal to binary
strconv.FormatInt(decimal, 2)

List

PHPPythonGolangTypeScript
Type namearrayListSlice
Initialize
$list = []
list = []
slice := make([]int, length) or slice := make([]int, length, capacity)
Add an element
$list[] = $element
list.append(element)
slice = append(slice, element)
Sort (ascending)
sort($list)
list.sort()
# or
sortedList = sorted(list)
sort.Ints(s)
# or
sort.Slice(slice, func (i, j int) bool {
  return slice[i] < slice[j]
})
Sort (descending)
rsort($list)
list.sort(reverse=True)
# or
sortedList = sorted(list, reverse=True)
sort.Reverse(sort.Sort(slice))

Hash

PHPPythonGolangTypeScript
TypearrayDictionaryMap
Initialize
$hash = []
dictionary = {}
m := make(map[string]int)
# or
slice := make(map[string]int, capacity)
Add
$hash[$key] = $value
dictionary[key] = value
m[key] = value
Check if a key exists
array_key_exists($key, $hash)
key in dictionary
value, exists := m[key]

Queue

PHPPythonGolangTypeScript
Typecollections.dequeSlice
Initialize
queue = deque([1, 2])
Enqueue
queue.append(element)
Dequeue
queue.pop()
Length
len(queue)
Empty
bool(queue)

Heap

PHPPythonGolangTypeScript
Typeheap.Interfacecontainer.heap.Interface
Initialize
heap.Init(h heap.Interface)
Enqueue
heap.Push(h heap.Interface, value any)
Dequeue
heap.Pop(h heap.Interface) any

Golang implementation for Heap

Golang needs to implement the interface [container.heap.Interface] to use a heap or a priority queue.

// An Item is something we manage in a priority queue.
type Item struct {
	value    string
	priority int

}

type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *PriorityQueue) Push(x any) {
	n := len(*pq)
	item := x.(*Item)
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() any {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	*pq = old[0 : n-1]
	return item
}

Tools

Package managers / Virtual environments

PHPPythonGolangTypeScript
ComposerPDMPoertyHatchcondavenv modulego modulenpmyarnpnpm
Support different language versionsNoYesNo
How to set up
python -m venv /path/to/venv
source /path/to/venv/bin/activate.$SHELL_NAME

For Python, see some articles like this dev.to article for better comparisons.

Last updated on