Working with Constants and iota in Go.
The Constant Generator iota
Here’s an example from the time package, which defines named constants of type Weekday for the days of the week, starting with zero for Sunday. Types of this kind are often called en...
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 (
"...
Golang path and filepath Examples.
Using filepath.Base
The number is the index of the last slash in the string. If you want to get the file's base name, use filepath.Base:
package main
import (
"fmt"
"path/filepath"
)
func main() {
path ...
In Python, using the timedelta object is the easiest way to calculate number of days between two given dates.
Using timedelta
If you have two date objects, you can just subtract them, which computes a timedelta object.
In this program, we will cal...
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 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 Rust, there are 2 ways to remove an element from a vector.
Using retain Function
The vec::retain() function retains only the elements specified by the predicate.
For example,
fn main() {
let mut v = vec![1, 3, 2, 3, 4];
v.retain(|&x| x ...
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, there are 3 ways to get file creation and modification dates times.
Using os.path Function
The os.path.getmtime() function returns the time of last modification of path.
The os.path.getctime() function returns the system’s ctime which, o...
In Python, there are 2 ways to get the filename without the extension from a path.
Using pathlib Module
You can use .stem from pathlib to get the filename without the extension from a path, it really simple. For example,
#!/usr/bin/python3
# Im...
In Python, there are 2 ways to extract extension from filename.
Using os.path.splitext Method
The os.path.splitext(path) method splits the pathname path into a pair (root, ext) such that root + ext == path, and the extension, ext, is empty or begins...
In Python, there are 3 ways to remove an element from a list by index.
Using del Keyword
There is a way to remove an item from a list given its index instead of its value. For example,
#!/usr/bin/python3
# -*- coding: utf8 -*-
l = [1, 2, 3, 4]...
In Python, there are 2 ways to get the current time.
Using datetime Module
The datetime module supplies classes for manipulating dates and times. See the following example:
#!/usr/bin/python3
# -*- coding: utf8 -*-
# Import module
import datet...
In Golang, there are 2 ways to assign string to bytes.
Using byte Function
The easiest way to convert string to []byte in Golang:
package main
import "fmt"
func main() {
s := "Hello"
b := []byte(s)
fmt.Printf("%v\n", b)
}
[72 101 10...
In Python, there are 3 ways to merge two dictionaries into a new dictionary.
Using Additional Unpacking Generalizations
Starting with Python 3.5, we can merge in with literal notation as well. See the following example:
#!/usr/bin/python3
# -*-...