Several Ways to Stop Goroutine in Go.
A signal channel
Typically, you pass the goroutine a (possibly separate) signal channel. That signal channel is used to push a value into when you want the goroutine to stop. The goroutine polls that channel reg...
Defer, Panic, and Recover in Go.
panic
During a typical panic, normal execution stops, all deferred function calls in that goroutine are executed, and the program crashes with a log message.
package main
func main() {
panic("invalid")
}
...
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 Golang, using the crypto/aes package is the easiest way to encrypt a string using AES CBC.
Ehrsam, Meyer, Smith and Tuchman invented the cipher block chaining (CBC) mode of operation in 1976. In CBC mode, each block of plaintext is XORed with the ...
In Golang, using the crypto/aes package is the easiest way to encrypt a string using AES GCM 256.
In cryptography, Galois/Counter Mode (GCM) is a mode of operation for symmetric-key cryptographic block ciphers which is widely adopted for its performa...
In Golang, using the IsPrivate function is the easiest way to check if IP address is in private network space.
Using IsPrivate Function
The IsPrivate() function reports whether ip is a private address, according to RFC 1918 (IPv4 addresses) and RFC...
In Python, there are 3 ways to get the position of a character.
Using find Method
The str.find(sub[, start[, end]]) method returns the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start an...
In Python, there are 2 ways to find out the number of CPUs.
Using multiprocessing Module
The multiprocessing.cpu_count() method returns the number of CPUs in the system. For example,
#!/usr/bin/python3
# Import module
import multiprocessing
c...
In Golang, there are 2 ways to change the current directory.
Using Dir Property
Usually if you need a command to run from a specific directory, you can specify that as the Dir property on the Command, for example:
package main
import (
"os/...
In Golang, using the Readdirnames function is the easiest way to check if directory on path is empty.
Using Readdirnames Function
The file.Readdirnames(n int) function reads the contents of the directory associated with file and returns a slice of ...
In Golang, there are 3 ways to remove invalid UTF-8 characters from a string.
Using ToValidUTF8 Function
The strings.ToValidUTF8() function returns a copy of the string s with each run of invalid UTF-8 byte sequences replaced by the replacement str...
In Python, using the os.environ variable is the easiest way to set environment variables
Using os.environ Variable
os.environ behaves like a python dictionary, so all the common dictionary operations can be performed. In addition to the get and set ...
In Python, there are 2 ways to retrieve a module's path.
Using __file__ Attribute
You can retrieve a module's path using the __file__ attribute. For example,
#!/usr/bin/python3
# Import module
import os
print(os.__file__)
/usr/local/lib/pyt...
In Python, using the str.join function is the easiest way to concatenate a list of strings into a single string
Using str.join Function
The str.join() method returns a string which is the concatenation of the strings in iterable. A TypeError will be...
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
#...