Cheat sheet for programming languages
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 TypeScript2 variables have the same types Syntax for types
PHP Python Golang TypeScript Type Alias 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
PHP Python Golang TypeScript 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
PHP Python Golang TypeScript Decimal to binary strconv . FormatInt ( decimal , 2 )
List
PHP Python Golang TypeScript Type name array List Slice Initialize slice := make ([] int , length ) or slice := make ([] int , length , capacity )
Add an element slice = append ( slice , element )
Sort (ascending) 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) list . sort ( reverse = True )
# or
sortedList = sorted ( list , reverse = True )
sort . Reverse ( sort . Sort ( slice ))
Hash
PHP Python Golang TypeScript Type array Dictionary Map Initialize m := make ( map [ string ] int )
# or
slice := make ( map [ string ] int , capacity )
Add Check if a key exists array_key_exists ( $key , $hash )
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 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.
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 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.