Cheat sheet for programming languages
Cheat sheet for programming languages
April 28, 2024
Basics
| PHP | Python | Golang | TypeScript | |
|---|---|---|---|---|
| null | None | nil | null | |
| Undefined key | undefined | |||
| Equal | | | | |
| Strict equal | | | ||
| Null Coalescence | | | ||
| Null Assertion (Ignore null value) | |
- Strict equal in TypeScript
- 2 variables have the same types
Syntax for types
| PHP | Python | Golang | TypeScript | |
|---|---|---|---|---|
| Type Alias | | | ||
| Interface | | |
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
| PHP | Python | Golang | TypeScript | |
|---|---|---|---|---|
| if, else if, else | | | | |
| for | | | | |
| foreach for an array without a index | | | | |
| foreach for an array with an index | | | | |
| foreach for a hash | | | |
Operations for common types
Numbers
| PHP | Python | Golang | TypeScript | |
|---|---|---|---|---|
| Decimal to binary | |
List
| PHP | Python | Golang | TypeScript | |
|---|---|---|---|---|
| Type name | array | List | Slice | |
| Initialize | | | | |
| Add an element | | | | |
| Sort (ascending) | | | | |
| Sort (descending) | | | |
Hash
| PHP | Python | Golang | TypeScript | |
|---|---|---|---|---|
| Type | array | Dictionary | Map | |
| Initialize | | | | |
| Add | | | | |
| Check if a key exists | | | |
Queue
| PHP | Python | Golang | TypeScript | |
|---|---|---|---|---|
| Type | collections.deque | Slice | ||
| Initialize | | |||
| Enqueue | | |||
| Dequeue | | |||
| Length | | |||
| Empty | |
Heap
| PHP | Python | Golang | TypeScript | |
|---|---|---|---|---|
| Type | heap.Interface | container.heap.Interface | ||
| Initialize | | |||
| Enqueue | | |||
| Dequeue | |
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
| PHP | Python | Golang | TypeScript | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Composer | PDM | Poerty | Hatch | conda | venv module | go module | npm | yarn | pnpm | |
| Support different language versions | No | Yes | No | |||||||
| How to set up | | |||||||||
For Python, see some articles like this dev.to article for better comparisons.
Last updated on