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 Golang, using the cases package is the easiest way to make first letter of words uppercase in a string.
Using cases Package
install
Use the following command to download the repository to the local file system.
install
go mod tidy
...
In Golang, using the exec.Command() function is the easiest way to execute a shell command
Using exec.Command Function
Command returns the Cmd struct to execute the named program with the given arguments.
You can execute a shell command using the e...
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 Golang, there are 4 ways to generate a uuid.
Using uuid Package
install
Use the following command to download the repository to the local file system.
install
go mod tidy
go get github.com/google/uuid
go install github.com...
In Golang, there are 2 ways to list all standard packages.
Using packages
go install
Use the following command to download the repository to the local file system.
install
go get golang.org/x/tools/go/packages
go install golang.or...
In Python, there are 3 ways to list all files of a directory.
Using os.listdir Method
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries '.' and ...
In Python, there are 3 ways to execute a program or call a system command.
Using subprocess Module
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. See the following e...
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 Python, there are 3 ways to split a list into equally sized chunks.
Using List Comprehension
List comprehensions provide a concise way to create lists. The following example:
#!/usr/bin/python3
# -*- coding: utf8 -*-
# initialize
lst = ['a'] *...
In Golang, there are 2 ways to install Go in Linux.Download Packages
Download packages for Windows 64-bit, macOS, Linux, and more.
Go install
Linux
Remove any previous Go installation by deleting the /usr/local/go folder (if it exists), then extr...
In Rust, "Hello World!" is the first basic program.
Build and Run
In our main.rs, add the following code:
// main.rs
fn main() {
println!("Hello,世界,こんにちは,안녕하세요")
}
Hello,世界,こんにちは,안녕하세요
we can run our application by typing:
B...
In Rust, there are two ways to install rust in Linux.Running the following in your terminal
If you’re running macOS, Linux, or another Unix-like OS. To download Rustup and install Rust, run the following in your terminal, then follow the on-screen i...
Converting Json to string in Python.Decoding JSON
To convert string to json in Python, use the json.loads() function. The json.loads() is a built-in Python function that accepts a valid json string and returns a dictionary to access all elements. The...
Hello, World! is the first basic program in any programming language.Python Version
Print the Python version number and exit. Example output could be:
Python version
python -V
python --version
Python 3.9.1
Running Files with Command Lines
You c...