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

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 Get a SHA256 Hash from a String in Go

In Golang, using the Sum256 function is the easiest way to get a SHA256 hash from a string.Go implements several hash functions in various crypto/* packages. Using Sum256 Function The sha256.Sum256() function returns the SHA256 checksum of the data...
ada

How to Get a MD5 Hash from a String in Go

In Golang, using the Sum function is the easiest way to get a MD5 hash from a string. Using Sum Function The md5.Sum() function returns the MD5 checksum of the data. The following example should cover whatever you are trying to do: package mai...
aweis

How to Do Case Insensitive String Comparison in Python

In Python, there are 2 do case insensitive string comparison. Using casefold Method The str.casefold() method returns a casefolded copy of the string. Casefolded strings may be used for caseless matching. For example, #!/usr/bin/python3 a = "H...
pooriabt

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 Pipe Several Commands in Go

In Golang, there are 2 ways to pipe several commands. Using piped commands For example, this function retrieves the CPU model name using piped commands: package main import ( "fmt" "os/exec" ) func main() { cmd := "cat /proc/cpuinfo | eg...
Patcher56

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 Remove all Whitespace in a String in Python

In Python, there are 3 ways to remove all whitespace in a string. Using str.replace Function The str.replace(x) function returns a copy of the string with all occurrences of substring old replaced by new. See the following example: #!/usr/bin/p...
pooriabt

How to Copy a Dictionary in Python

In Python, there are 4 ways to copy a dictionary. Using dict Method If you want to copy the dict, you have to do so explicitly with: #!/usr/bin/python3 a = {"a": 1, "b": 2} # Copy b = dict(a) b['a'] = 3 print(a) print(b) {'a': 1, 'b': 2} {'...
Patcher56

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 Download a Large File in Go

In Golang, using the io.copy function is the easiest way to download a large file Using io.Copy Function The io.Copy() function copies from src to dst until either EOF is reached on src or an error occurs. It returns the number of bytes copied and ...
ada

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 Remove a Trailing Newline in Python

In Python, there are 4 ways to remove a trailing newline. Using rstrip Method The str.rstrip() method returns a copy of the string with trailing characters removed. For example, #!/usr/bin/python3 s = "\nab c\r\n" # str.rstrip([chars]) n = s....
Tomoki

How to Read a Whole File into a String Variable in Go

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