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 ...
Calculate degrees celcius from farenheit in Go.
Following program shows you how to convert fahrenheit to celsius.
The function FToC below encapsu- lates the temperature conversion logic so that it is defined only once but may be used from multiple p...
Understanding Rune in Go.
UTF-8 Decoder
The unicode/utf8 package provides one that we can use like this:
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
s := "Hello, 世界"
for i := 0; i < len(s); {
r, size := utf8.DecodeR...
In Python, there are 2 ways to extract all the numbers contained in a string.
Using List Comprehension
List comprehensions provide a concise way to create lists.
If you only want to extract only positive integers, try the following:
#!/usr/bin/...
In Python, there are 2 ways to capitalize the first letter of each word in a string.
Using title Method
The str.title() method returns a titlecased version of the string where words start with an uppercase character and the remaining characters are...
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...
In Python, there are 2 ways to get an absolute file path.
Using abspath Function
The os.path.abspath() method returns a normalized absolutized version of the pathname path. For example,
#!/usr/bin/python3
# Import module
import os
abs = os.pat...
In Python, using the uuid module is the easiest way to create a GUID/UUID.
Using uuid Module
uuid module provides immutable UUID objects (the UUID class) and the functions uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 UUID...
In Python, there are 3 ways to check what version of the Interpreter is interpreting my script.
Using sys.version String
A string containing the version number of the Python interpreter plus additional information on the build number and compiler 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 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...
In Rust, there are 3 ways to convert a string into a &'static str.
Using slicing Syntax
To go from a String to a slice &'a str you can use slicing syntax. See the following example:
// Rust 1.0+
fn main() {
let s: String = "abc".to_owned();
...
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 remove packages installed with go get.
Using go mod tidy
go mod tidy ensures that the go.mod file matches the source code in the module. It adds any missing module requirements necessary to build the current module’s p...
In Golang, there are 3 ways to get a slice of keys from a map.Using For Range
The easiest way to get keys of map in Golang:
package main
import "fmt"
func main() {
m := map[string]int{
"a": 1,
"b": 1,
"c": 1,
}
// minimize ...