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 sort a vector in Rust

In Rust, there are 2 ways to sort a vector. Using sort Method The sort(&mut self) method sorts the slice. This sort is stable (i.e., does not reorder equal elements) and O(n * log(n)) worst-case. For example, fn main() { let mut v = [3, -5, ...
Tomoki

How to Check if a Slice Contains an Element in Go

In Golang, there are 2 ways to check if a slice contains an element. Using contains Function The following example should cover whatever you are trying to do: package main import "fmt" func Contains[E comparable](s []E, v E) bool { for _, ...
ada

How to Sort a Slice in Go

In Golang, using the sort.Slice function is the easiest way to sort a slice Using Slice Function You simply pass an anonymous function to the sort.Slice function: package main import ( "fmt" "sort" ) func main() { s := []string{"Houston...
Tomoki

How to Make a Shallow Copy of a Slice in Go

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

How to Delete a Character from a String in Python

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

How to Convert a String to int in Rust

In Rust, using the parse method is the easiest way to convert a string to int. Using parse Method The str::parse::<T>() method parses this string slice into another type. For example, fn main() { let s = "100".to_string(); println!("{}"...
Tomoki

How to Get the Psition of a Character in Python

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

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 the First Character of a String in Go

In Golang, using the DecodeRuneInString function is the easiest way to remove the first character of a string. Using DecodeRuneInString Function The following example should cover whatever you are trying to do: package main import ( "fmt" ...
Patcher56

How to Print the Memory Address of a Slice in Go

In Golang, using the %p verbs is the easiest way to print the memory address of a slice. Using fmt.Printf Function Pointer: %p base 16 notation, with leading 0x. The %b, %d, %o, %x and %X verbs also work with pointers, formatting the value exactly ...
Tomoki

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 Access Command-Line Arguments in Go

In Golang, there are 2 ways to access command-line arguments passed to a program. Using os.Args Variable You can access the command-line arguments using the os.Args variable. Note that the first value in this slice is the path to the program, and o...
Unused

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 Iterate Over a Slice in Reverse in Go

In Golang, there are 2 ways to iterate over a slice in reverse. Using For Loop There is no convenient operator for this to add to the range one in place. You'll have to do a normal for loop counting down. See the following example: package main...
Patcher56