All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to Use Deferred Function Calls in Golang

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...
ada

How to Use Variadic Functions in Golang

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...
Patcher56

How to Use Anonymous Functions in Golang

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 ...
Patcher56

How to Format a Currency with Commas in Golang

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 ( "...
Unused

How to Delete a Character from a String in Python

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...
Unused

How to Get the Psition of a Character in Python

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...
aweis

How to Append Integer to Beginning of List in Python

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...
pooriabt

How to Capitalize the First Letter of each Word in a String in Python

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...
Patcher56

How to Subtract a Day from a Date in Python

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 #...
Sambhav Khandelwal

How to Execute a Shell Command in Go

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...
aweis

How to Access Command-Line Arguments in Go

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...
Unused

How to Count the Items in the Map in Go

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...
Sambhav Khandelwal

How to Check If an Object Is of a Given Type in Python

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...
Unused

How to Unpack Array as Arguments in Go

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...
Unused

How to Remove a Trailing Newline in Python

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....
Tomoki