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

How to Compare if two Maps are Equal in Golang

Testing the equivalence of maps in Go. reflect.DeepEqual Function It first checks if both maps are nil, then it checks if they have the same length before finally checking to see if they have the same set of (key, value) pairs. package main impo...
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 Encrypt and Decrypt a String Using AES CBC in Go

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

How to Encrypt and Decrypt a String Using AES GCM 256 in Go

In Golang, using the crypto/aes package is the easiest way to encrypt a string using AES GCM 256. In cryptography, Galois/Counter Mode (GCM) is a mode of operation for symmetric-key cryptographic block ciphers which is widely adopted for its performa...
Tomoki

How to Generate a SHA256 HMAC Hash from a String in Go

In Golang, using the crypto/hmac library is the easiest way to generate a SHA256 HMAC Hash from a string. In cryptography, an HMAC (hash-based message authentication code) is a specific type of message authentication code (MAC) involving a cryptograp...
ada

How to Count Characters in a String in Go

In Golang, there are 2 ways to count characters in a string. Using RuneCountInString Function Straight forward natively use the utf8.RuneCountInString() The following example should cover whatever you are trying to do: package main import ( ...
aweis

How to Get the String Length in Characters in Rust

In Rust, there are 2 ways to get the string length in characters. Using chars Function The str::chars() function returns an iterator over the chars of a string slice. See the following example: fn main() { let mut s = "Hello"; println!("{}...
Tomoki

How to Get File Length in Go

In Golang, there are 2 ways to get file length. Using file.Stat Function Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY. See the followin...
ada

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 Reverse a String in Python

In Python, there are 3 ways to reverse a string. Using slicing Slice notation takes the form [start:stop:step]. #!/usr/bin/python3 s = "Hello World" print(s[::-1]) # syntax # a[start:stop] # items start through stop-1 # a[start:] # items...
pooriabt

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 Pad a Numeric String with Zeros to the Left in Python

In Python, there are 3 ways to pad a numeric string with zeros to the left. Using zfill Method The str.zfill(width) method returns a copy of the string left filled with ASCII '0' digits to make a string of length width. For example, #!/usr/bin/...
Patcher56

How to Check the Equality of two Slices in Go

In Golang, there are 3 ways to check the equality of two slices. Using bytes.Equal Function Equal reports whether a and b are the same length and contain the same bytes. A nil argument is equivalent to an empty slice. For example, package main...
aweis

How to Convert Byte Array to String in Go

In Golang, there are 2 ways to convert byte array to string.Using string Function The easiest way to convert []byte to string in Golang: package main import "fmt" func main() { b := []byte{'a', 'b', 'c'} s := string(b[:]) fmt.Printf("%...
Tomoki

How to Read a File Line-by-Line in Go

In Golang, there are 2 ways to read a file line-by-line. Using bufio.Scanner Function Scanner provides a convenient interface for reading data such as a file of newline-delimited lines of text. The ones that are currently implemented are: packa...
Sambhav Khandelwal