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

How to Use bytes.Buffer in Golang

Golang bytes.Buffer Examples Examples of Golang bytes.Buffer The bytes package provides the Buffer type for efficient manipulation of byte slices. A Buffer starts out empty but grows as data of types like string, byte, and []byte are written to it. ...
ada

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 Remove Invalid UTF-8 Characters from a String in Go

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

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 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 all Empty Strings from a List of Strings in Python

In Python, there are 3 ways to remove all empty strings from a list of strings. Using filter Function The filter(function, iterable) function constructs an iterator from those elements of iterable for which function returns true. The following exam...
Sambhav Khandelwal

How to Get the System Hostname in Python

In Python, there are 3 ways to get the system hostname. Using gethostname Method The socket.gethostname() method returns a string containing the hostname of the machine where the Python interpreter is currently executing. For example, #!/usr/bin...
Tomoki

How to Get the Filename Without the Extension from a Path in Python

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...
Sambhav Khandelwal

How to Extract Extension from Filename in Python

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

How to Randomly Select a Element from List in Python

In Python, there are 3 ways to randomly select a element from list. Using random.choice Method The random.choice() method returns a random element from the non-empty sequence seq. If seq is empty, raises IndexError. For example, #!/usr/bin/pyth...
Tomoki

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 Concatenate Vectors in Rust

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]...
Unused

How To Make a For Loop and Range in Rust

In Rust, there are 3 ways to support For Loop expression.Using while Expression A while loop begins by evaluating the boolean loop conditional operand. fn main() { let mut i = 0; while i < 3 { println!("{}", i); i += 1; } // Vec ...
ada

How to check if a map is empty in Go

2 ways to check if a map is empty in Golang.Using len Function You can use the len function to check if a map is empty. it returns the length of v, according to its type. len(s) map[K]T: map length (number of defined keys) package main import "fmt"...
Tomoki

How to split a string into a slice in Go

Splitting a String into a Slice in Golang.Using strings.Split Function Use the strings.Split function to split a string into its comma separated values. package main import ( "fmt" "strings" ) func main() { s := "Japan,Germany" // func S...
Unused