Ben Chuanlong Du's Blog

It is never too late to learn.

Hands on the filepath Library in Golang

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

In [6]:
import "os"
import "fmt"
import "path/filepath"
In [2]:
filepath.Dir("/home/myname/")
Out[2]:
/home/myname
In [3]:
filepath.Dir("/home/myname")
Out[3]:
/home
In [2]:
filepath.Base("/usr/local/bin")
Out[2]:
bin
In [3]:
filepath.Base("/usr/local/bin/abc.txt")
Out[3]:
abc.txt
In [21]:
filepath.Base("https://www.legendu.net")
Out[21]:
www.legendu.net
In [2]:
filepath.Join("/usr/", "local/bin")
Out[2]:
/usr/local/bin

filepath.Glob

Note: The glob package in Python and Rust behave similarly. Both of them find all files and subdirectories for the pattern **/*. However, filepath.Glob behaves different from those 2 packages. filepath.Glob treats **/* as */*.

In [19]:
entries, err := filepath.Glob("./**/*")
entries
Out[19]:
[.ipynb_checkpoints/abc-checkpoint.txt .ipynb_checkpoints/hands-on-the-filepath-library-in-golang-checkpoint.ipynb test/.ipynb_checkpoints test/test.txt test/test2]
In [20]:
for _, entry := range entries {
    fmt.Printf("%s\n", entry)
}
.ipynb_checkpoints/abc-checkpoint.txt
.ipynb_checkpoints/hands-on-the-filepath-library-in-golang-checkpoint.ipynb
test/.ipynb_checkpoints
test/test.txt
test/test2
In [ ]:
 

Comments