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

How to Use Strings in Golang

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

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 Put the Current Thread to Sleep in Rust

In Rust, using the thread::sleep_ms method is the easiest way to put the current thread to sleep. Using sleep_ms Method The thread::sleep_ms() method puts the current thread to sleep for at least the specified amount of time. For example, use ...
Sambhav Khandelwal

How to Check if Directory on Path is Empty in Go

In Golang, using the Readdirnames function is the easiest way to check if directory on path is empty. Using Readdirnames Function The file.Readdirnames(n int) function reads the contents of the directory associated with file and returns a slice of ...
Sambhav Khandelwal

How to Check if a File is a Valid Image in Go

In Golang, there are 2 ways to check if a file is a valid image. Using DetectContentType Function The http.DetectContentType() function implements the algorithm described at https://mimesniff.spec.whatwg.org/ to determine the Content-Type of the gi...
pooriabt

How to Clear a bytes.Buffer in Go

In Golang, using the buffer.Reset function is the easiest way to clear a bytes.Buffer Using buffer.Reset Function The buffer.Reset() function resets the buffer to be empty, but it retains the underlying storage for use by future writes. Reset is the...
Unused

How to Remove an Element from a Vector in Rust

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

How to List Files of a Directory in Rust

In Rust, using the read_dir function is the easiest way to list files of a directory. Using read_dir Function The fs::read_dir() function returns an iterator over the entries within a directory. See the following example: use std::fs; fn main...
ada

How to Copy a File in Go

In Golang, there are 2 ways to copy a file. Using io.Copy Function You can copy a file using the io.Copy() function. For example, package main import ( "io" "os" ) // Copy the src file to dst. func Copy(src, dst string) error { in, err...
Unused

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 Append Text to a File in Go

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

How to Prettyprint a JSON File in Python

In Python, there are 2 ways to prettyprint a JSON file. Using json module The json module already implements some basic pretty printing in the dump and dumps functions, with the indent parameter that specifies how many spaces to indent by: #!/us...
Patcher56

How to Create Nested Directories in Go

In Golang, using the os.MkdirAll function is the easiest way to create nested directories Using os.MkdirAll Function The os.MkdirAll() function creates a directory named path, along with any necessary parents, and returns nil, or else returns an er...
Sambhav Khandelwal

How to Set Timeout for http.Get Requests in Go

In Golang, there are 2 ways to set timeout for http.Get requests Using http.Client.Timeout Field Timeout specifies a time limit for requests made by this Client. The timeout includes connection time, any redirects, and reading the response body. S...
Tomoki