Understanding defer in Go.
Deferred Function Calls
A defer statement is an ordinary function or method call prefixed by the keyword defer. The function and argument expressions are evaluated when the statement is executed, but the actual call is def...
In Python, using the __file__ is the easiest way to get name of current script.
Using Module Attributes
You can use __file__ to get the name of the current file. When used in the main module, this is the name of the script that was originally invoke...
In Golang, using the Unmarshal JSON function is the easiest way to Marshal / Unmarshal json with a custom type attribute.
JSON (JavaScript Object Notation) is a lightweight data-interchange format.
Using UnmarshalJSON Function
Example on custom Unm...
In PHP, using the $_SERVER variable is the easiest way to get the full URL.
Using $_SERVER Variable
$_SERVER is an array containing information such as headers, paths, and script locations.
The following example should cover whatever you are trying...
In Golang, there are 2 ways to escape the html.
Using EscapeString Function
The html.EscapeString() function escapes special characters like "
In Python, there are 3 ways to get the size of a file.
Using os.path.getsize Method
The os.path.getsize() method returns the size, in bytes, of path. Raise OSError if the file does not exist or is inaccessible. For example,
#!/usr/bin/python3
#...
In Python, there are 3 ways to get file creation and modification dates times.
Using os.path Function
The os.path.getmtime() function returns the time of last modification of path.
The os.path.getctime() function returns the system’s ctime which, o...
In Golang, there are 2 ways to get file length.
Using file.Stat Function
Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY.
See the followin...
In Python, there are 3 ways to check what version of the Interpreter is interpreting my script.
Using sys.version String
A string containing the version number of the Python interpreter plus additional information on the build number and compiler us...
In Python, there are 3 ways to check if a directory exists.
Using os.path.isdir Method
You can use os.path.isdir() to check if a directory exists, it really simple. For example,
#!/usr/bin/python3
# Import module
import os
d = "./folder"
print...
In Python, there are 2 ways to make a time delay.
Using time.sleep Function
This function actually suspends execution of the calling thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep...
In Python, there are 3 ways to check if a file exists.
Using os.path.exists Method
Return True if path refers to an existing path or an open file descriptor. Returns False for broken symbolic links. On some platforms, this function may return False ...
In Golang, there are 2 ways to check if a file exists.Using os.Stat Function
Stat returns a FileInfo describing the named file. See the following example:
package main
import (
"errors"
"fmt"
"os"
)
func main() {
// since the additio...
Hello, World! is the first basic program in any programming language.Python Version
Print the Python version number and exit. Example output could be:
Python version
python -V
python --version
Python 3.9.1
Running Files with Command Lines
You c...
How do you check if a string contains a substring?.Using String.prototype.includes
The includes() method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.
'Go...