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

How to Recover panic in Golang

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") } ...
Unused

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 Add Two Numbers in Go

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

How to Determine Whether a Given Integer is Between two Other Integers in Python

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

How to Extract All the Numbers Contained in a String in Python

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

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 Get Current Time in Milliseconds in Python

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

How to Get the Day of Week Given a Date in Python

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

How to Convert Float to Integer in Rust

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

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 Get the ASCII Value of a Character as an Int in Python

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

How to Check the Version of the Interpreter in Python

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

How to Index Characters in a String in Go

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

How to Convert an Integer to a Float Number in Go

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

How to Generate Random Integers Between 0 and 9 in Python

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