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...
Variadic Functions Tutorial with Examples in Go.
Variadic Function
A variadic function is one that can be called with varying numbers of arguments. The most familiar examples are fmt.Printf and its variants.
package main
import "fmt"
func add(v...
Closures and Anonymous Functions in Go.
Anonymous Function
A function literal is written like a function declaration, but without a name following the func keyword. It is an expression, and its value is called an anonymous function.
package main
...
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 (
"...
In Python, there are 2 ways to delete a character from a string.
Using replace Method
The str.replace(old, new[, count]) method returns a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is giv...
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 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 2 ways to capitalize the first letter of each word in a string.
Using title Method
The str.title() method returns a titlecased version of the string where words start with an uppercase character and the remaining characters are...
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 Golang, using the exec.Command() function is the easiest way to execute a shell command
Using exec.Command Function
Command returns the Cmd struct to execute the named program with the given arguments.
You can execute a shell command using the e...
In Golang, there are 2 ways to access command-line arguments passed to a program.
Using os.Args Variable
You can access the command-line arguments using the os.Args variable.
Note that the first value in this slice is the path to the program, and o...
In Golang, using the len function is the easiest way to count the items in the map structure.
Using len Function
The built-in function len take arguments of various types and return a result of type int. The implementation guarantees that the result...
In Python, there are 2 ways to check if an object is of a given type.
Using isinstance Method
The isinstance(object, classinfo) method returns True if the object argument is an instance of the classinfo argument, or of a (direct, indirect, or virtua...
In Golang, there are 2 ways to unpack array as arguments.
Using Variadic Functions
Variadic functions can be called with any number of trailing arguments. Here’s a function that will take an arbitrary number of ints as arguments. For example,
p...
In Python, there are 4 ways to remove a trailing newline.
Using rstrip Method
The str.rstrip() method returns a copy of the string with trailing characters removed. For example,
#!/usr/bin/python3
s = "\nab c\r\n"
# str.rstrip([chars])
n = s....