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

How to Get correct file base name in Golang

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

How to Convert the Fahrenheit to Celsius in Golang

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...
Sambhav Khandelwal

How to Convert String to Rune in Golang

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

How to Extract All the Numbers Contained in a String in Python

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/...
Sambhav Khandelwal

How to Capitalize the First Letter of each Word in a String in Python

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

How to Get the System Hostname in Python

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

How to Get an Absolute File Path in Python

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

How to Create a GUID/UUID in Python

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

How to Check the Version of the Interpreter in Python

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

How to Iterate through the Values of an Enum in Rust

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

How to Check the Equality of two Slices in Go

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

How to Convert a String into a &'static Str in Rust

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(); ...
Tomoki

How to Access Command Line Parameters in Rust

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::...
Patcher56

How to Remove Packages Installed with go get in Go

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

How to Get a Slice of Keys from a Map in Go

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 ...
Sambhav Khandelwal