Converting Error to String in Go Lang

Posted on

In Golang, fmt.Println(err) can print out the error err. But how to convert an error to a string explicitely? In Go, the error type is an interface type that has an interface type error interface { Error() string } An error variable represents any value that can describe itself as a string. Ref: https://golang.org/pkg/builtin/#error So,
Read more

How to process a file line by line in Go?

Posted on

In the Go programming language, How to process a file line by line? An equivalent piece of Bash code could be while read line ; do echo $line done < ./input.txt You can make use of the bufio package’s Scan(). // open the file filepath f := os.Open(filepath) // create a scanner fs := bufio.NewScanner(f)
Read more

How to get the full path and directory of a Makefile itself?

Posted on

How to get the full path and directory of a Makefile itself like finding Bash script’s own path? This 2 lines in Makefile get the full path and dir of the Makefile itself: mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) mkfile_dir := $(dir $(mkfile_path)) The piece of code gets you Makefile, finds its absolute path and the
Read more

How to get the CPU temperatur in command linux on Linux?

Posted on

Most modern servers or computers have sensors to detect the temperature of various components. On Linux, how to get the CPU core temperatur in command linux? First, make sure the package “lm-sensors” is installed on your Linux and the command sensors works for you. Then, you can use this piece of script to get the
Read more

How to test whether PATH has a specific directory in it in Bash on Linux?

Posted on

How to test whether PATH has a specific directory in it in Bash on Linux? For example, how to test whether /usr/local/mytool/bin is in $PATH? Directories in the $PATH variable are separated by :. So you can use a regular expression to test whether the $PATH contains a directory. The code for your specific example
Read more

How to test a file or directory exists in Python?

Posted on

In Bash, the [ -f ] and [ -d ] tests can test whether a file or a directory exist. What are the corresponding python method for these tests? For -f: **os.path.isfile(path)** Return True if path is an existing regular file. This follows symbolic links. For -d: **os.path.isdir(path)** Return True if path is an existing
Read more

How to get file extension in JavaScript?

Posted on

How to get file extension from a file name in JavaScript? For examples, For “file.txt”, I want to get “txt”. For “file2.multi.ext.fileext”, I want to get “fileext”. This JavaScript function works well for me. function getExt(filename) { var idx = filename.lastIndexOf(‘.’); // handle cases like, .htaccess, filename return (idx < 1) ? “” : filename.substr(idx
Read more

How to get a user’s home directory from the username in a variables in Bash?

Posted on

In Bash, ~username gives the username user’s home. However, this does not work for situations where username is in a variable such as ~$user How to get the home directory from the user name in a variable in Bash? You can use this Bash script snippet: userhome=$(eval echo ~$user) Here, $user get evaluated to its
Read more

In Vim, how to search and replace in visual selection?

Posted on

Search and replace in the whole text in Vim can be done by :%s/from/to/gc. But how to search and replace in visual selection only? The %V atom in Vim restricts a pattern so that it matches only inside the visual selection. So, your command for searching and replacing only in the visual selection can be
Read more