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 Golang, there are 2 ways to duplicate slices.
Using append Function
You could write one simple statement to make a shallow copy of a slice.
The following example should cover whatever you are trying to do:
package main
import "fmt"
type T...
In Golang, using the crypto/aes package is the easiest way to encrypt a string using AES CBC.
Ehrsam, Meyer, Smith and Tuchman invented the cipher block chaining (CBC) mode of operation in 1976. In CBC mode, each block of plaintext is XORed with the ...
In Golang, using the IsPrivate function is the easiest way to check if IP address is in private network space.
Using IsPrivate Function
The IsPrivate() function reports whether ip is a private address, according to RFC 1918 (IPv4 addresses) and RFC...
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 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 Golang, using the os.O_APPEND flag is the easiest way to append text to a file
Using os.OpenFile Function
OpenFile is the generalized open call; most users will use Open or Create instead. It opens the named file with specified flag (O_RDONLY etc...
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 Golang, there are 3 ways to read a whole file into a string variable.
Using ioutil.ReadFile Function
ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads...
In Rust, there are 2 ways to concatenate vectors .
Using append Method
The method append() moves all the elements of other into Self, leaving other empty. See the following example:
fn main() {
let mut a = vec![1, 2];
let mut b = vec![3, 4]...
In Golang, there are 2 ways to pretty-print json.
Using json.MarshalIndent Function
MarshalIndent is like Marshal but applies Indent to format the output. Each JSON element in the output will begin on a new line beginning with prefix followed by one...
In Golang, there are 3 ways to get a slice of keys from a map.Using For Range
The easiest way to get keys of map in Golang:
package main
import "fmt"
func main() {
m := map[string]int{
"a": 1,
"b": 1,
"c": 1,
}
// minimize ...
In Rust, there are 4 ways to concatenate strings.
Using String.push_str Function
This function actually appends a given string slice onto the end of this `String`. See the following example:
fn main() {
let mut s1: String = "Hello ".to_owned(...
In Golang, there are 2 ways to concatenate two slices.Using append Function
The append built-in function appends elements to the end of a slice.
You can use append function, it really simple. For example,
package main
import "fmt"
func main(...
In Golang, there are 2 ways to remove duplicates strings from slice.Using a Hash Map
We have defined a function where we are passing the slice values and using the map function we are checking the duplicates and removing them. The ones that are curr...