Several Ways to Stop Goroutine in Go.
A signal channel
Typically, you pass the goroutine a (possibly separate) signal channel. That signal channel is used to push a value into when you want the goroutine to stop. The goroutine polls that channel reg...
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...
In Rust, there are 2 ways to check if a string contains whitespace.
Using char::is_whitespace
char::is_whitespace returns true if the character has the Unicode White_Space property.
You can pass char::is_whitespace to .contains():
fn main() {
...
In Python, using the os.environ variable is the easiest way to set environment variables
Using os.environ Variable
os.environ behaves like a python dictionary, so all the common dictionary operations can be performed. In addition to the get and set ...
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...
In Python, using the triple-quoted strings is the easiest way to create multiline comments.
Using triple-quoted Strings
String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''.
You can use triple-quoted strin...
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...
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...
In Rust, there are 2 ways to iterate through the values of an enum.
Using a static array
If the enum is C-like, then you can create a static array of each of the variants and return an iterator of references to them:
use self::Direction::*;
use...
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...
In Python, there are 3 ways to read from stdin.
Using fileinput Module
This module implements a helper class and functions to quickly write a loop over standard input or a list of files. For example,
#!/usr/bin/python3
# Import module
import f...
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....
In Rust, there are 2 ways to access command line parameters.
Using std::env::args Function
This function actually returns the arguments that this program was started with. See the following example:
use std::env;
fn main() {
for arg in env::...
In Golang, there are 3 ways to get the directory of the currently running file.Using os.Executable Function
Executable returns the path name for the executable that started the current process.
The easiest way to get the directory of the currently ...
In Golang, there are 2 ways to pretty-print json.
Using json.MarshalIndent Function
MarshalIndent is like Marshal but applies Indent to format the output. Each JSON element in the output will begin on a new line beginning with prefix followed by one...