Golang path and filepath Examples.
Using filepath.Base
The number is the index of the last slash in the string. If you want to get the file's base name, use filepath.Base:
package main
import (
"fmt"
"path/filepath"
)
func main() {
path ...
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, 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 Golang, there are 2 ways to read a file line-by-line.
Using bufio.Scanner Function
Scanner provides a convenient interface for reading data such as a file of newline-delimited lines of text. The ones that are currently implemented are:
packa...
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
...
Rust String Formatting and Printing Best Practices.Using println!
Prints to the standard output, with a newline. Use the format! syntax to write data to the standard output. See std::fmt for more information.
fn main() {
let name = "Bob";
pri...
Intersection set of golang slices.Using map cache
It's a best method for intersection two slice. Time complexity is too low. Time Complexity : O(m+n)
package main
import "fmt"
func intersection(a, b []string) ([]string, error) {
// uses empty s...
Comparing two slices in Go.Using map cache
Creates a slice of slice values not included in the other given slices. An implementation is shown in the section below.
package main
import "fmt"
// difference returns the elements in `a` that aren't in ...
List of Golang Keywords.
Keywords
The following keywords are reserved and may not be used as identifiers.
break default func interface select
case defer go map struct
chan else goto package switch
const ...