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")
}
...
Formatting float to currency string in Go.
Using localized formatting
Use golang.org/x/text/message to print using localized formatting for any language in the Unicode CLDR.
Make a main.go file containing the following:
package main
import (
"...
An example program using numbers.
Integers And Floating Point Numbers
Go has several different types to represent numbers. Generally we split numbers into two different kinds: integers and floating-point numbers.
Make a main.go file containing the ...
In Python, there are 2 ways to determine whether a given integer is between two other integers.
Using Comparisons
Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise o...
In Python, there are 2 ways to extract all the numbers contained in a string.
Using List Comprehension
List comprehensions provide a concise way to create lists.
If you only want to extract only positive integers, try the following:
#!/usr/bin/...
In Python, there are 2 ways to append integer to beginning of list.
Using insert Method
The list.insert() method inserts an item at a given position. The first argument is the index of the element before which to insert. For example,
#!/usr/bin...
In Python, there are 3 ways to get current time in milliseconds.
Using time Method
The time.time() method returns the time in seconds since the epoch as a floating point number. For example,
#!/usr/bin/python3
# Import module
import time
ms =...
In Python, there are 2 ways to get the day of week given a date.
Using weekday Method
The weekday() method returns the day of the week as an integer, where Monday is 0 and Sunday is 6.
The following example should cover whatever you are trying to ...
In Rust, there are 2 ways to convert float to integer.
Using as Keyword
Executing an as expression casts the value on the left-hand side to the type on the right-hand side.
An example of an as expression:
fn main() {
let a = 3.6415_f64;
l...
In Python, using the timedelta object is the easiest way to subtract a day from a date.
Using timedelta Object
A timedelta object represents a duration, the difference between two dates or times.
See the following example:
#!/usr/bin/python3
#...
In Python, using the ord function is the easiest way to get the ASCII value of a character as an int.
Using ord Function
The ord() function returns an integer representing the Unicode code point of that character.
To get the ASCII code of a charact...
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 Golang, there are 2 ways to index characters in a string.
Using individual characters
In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. Strings behave like slices of bytes. A rune is an integer value i...
In Golang, using the float64 function is the easiest way to convert an integer to a float number.
Using float64 Function
The easiest way to convert an integer to a float number in Golang:
package main
import "fmt"
func main() {
i := 6
f ...
In Python, there are 3 ways to generate random integers between 0 and 9.
Using randrange Method
The random.randrange(start, stop[, step]) method returns a randomly selected element from range(start, stop, step). For example,
#!/usr/bin/python3
...