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 ...
Introduction to Strings in Go.What is a String?
A string is an immutable sequence of bytes. Strings may contain arbitrary data, including bytes with value 0, but usually they contain human-readable text.
Make a main.go file containing the following...
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 PHP, there are 3 ways to delete an element from an array.
Using unset Function
The unset(mixed $var, mixed ...$vars): void function destroys the specified variables. For example,
$arr = ["a", "b", "d"];
unset($arr[1]);
print_r($arr);
Arr...
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 3 ways to get key by value in dictionary.
Using keys Function
Basically, it separates the dictionary's values in a list, finds the position of the value you have, and gets the key at that position.
The following example:
#!...
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 Golang, there are 2 ways to pipe several commands.
Using piped commands
For example, this function retrieves the CPU model name using piped commands:
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd := "cat /proc/cpuinfo | eg...
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 Rust, using the position function is the easiest way to find the index of an element in a vector.
Using position Method
The position() method searches for an element in an iterator, returning its index.
position() is short-circuiting; in other ...
In Rust, there are 2 ways to index a string.
Using as_bytes Function
If the enum is C-like, then you can create a static array of each of the variants and return an iterator of references to them:
fn main() {
let s = "abc";
let b: u8 = s.as...
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 Rust, there are 3 ways to update a value in a mutable HashMap.
Using get_mut Method
The get_mut() method returns a mutable reference to the value corresponding to the key. See the following example:
use std::collections::HashMap;
fn main()...