{"id":599,"date":"2022-04-29T13:31:00","date_gmt":"2022-04-29T13:31:00","guid":{"rendered":"https:\/\/andrejacobs.org\/?p=599"},"modified":"2022-05-27T15:53:54","modified_gmt":"2022-05-27T15:53:54","slug":"learning-go-day-9","status":"publish","type":"post","link":"https:\/\/andrejacobs.org\/study-notes\/learning-go-day-9\/","title":{"rendered":"Learning Go – Day 9"},"content":{"rendered":"\n
Featured image by Egon Elbre<\/a><\/p>\n\n\n\n In all my life of using mkdir -p, I never knew you can create multiple subdirectories the easy way. UPDATE:<\/strong> This is actually the shell doing some path expansion and thus can work anywhere.<\/p>\n Use You can also use Go comes with template rendering for HTML and Text.<\/p>\n<\/li>\n Learned a little bit about Colly – the web scraping package for Go.<\/p>\n<\/li>\n<\/ul>\n Gophers (Go programmers) use this concept called \u201cTable-Driven testing\u201d by which you build up a table of inputs and expected outputs and then in your unit-test you iterate over the table and check that feeding inputs produce the expected outputs.<\/p>\n I like this and can already see many places where I can apply the same idea to my Swift and other languages.<\/p>\n\n
$ mkdir -p ~\/temp\/some\/path\/{dir1,dir2,dir3\/even\/more}\n\n$ tree ~\/temp\/some\n\/Users\/andre\/temp\/some\n\u2514\u2500\u2500 path\n \u251c\u2500\u2500 dir1\n \u251c\u2500\u2500 dir2\n \u2514\u2500\u2500 dir3\n \u2514\u2500\u2500 even\n \u2514\u2500\u2500 more\n<\/code><\/pre>\n<\/li>\n
os.CreateTemp()<\/code> to create a temporary file on the system. You can specify a directory if needed and also a file name pattern the will be randomised. E.g \u201cconfig_*.json\u201d<\/p>\n<\/li>\n
os.os.MkdirTemp()<\/code> to create a temporary directory.<\/p>\n<\/li>\n
Working with file systems<\/h3>\n
\n
path\/filepath<\/code> to work agnostically with the system\u2019s file path. E.g. this takes into account that Windows uses
\\<\/code> versus Unix based os uses `<\/li>\n
filepath.Walk<\/code> to recursively walk a directory and it will call the function you specify on each found file or directory. There is a note that WalkDir performs better.\nSee the documentation for WalkFunc<\/a>.<\/li>\n<\/ul>\n
filepath.Walk(".\/somewhere\/here",\n\t\tfunc(path string, info fs.FileInfo, err error) error {\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\t\/\/ See the WalkFunc documentation for what this function can do to control\n\t\t\t\/\/ how the Walk behaves\n\t\t}\n}\n<\/code><\/pre>\n
\n
type FileInfo interface {\n\tName() string \/\/ base name of the file\n\tSize() int64 \/\/ length in bytes for regular files; system-dependent for others\n\tMode() FileMode \/\/ file mode bits\n\tModTime() time.Time \/\/ modification time\n\tIsDir() bool \/\/ abbreviation for Mode().IsDir()\n\tSys() any \/\/ underlying data source (can return nil)\n}\n<\/code><\/pre>\n
\n
filepath.Ext()<\/code><\/li>\n<\/ul>\n
import "path\/filepath"\n\nfmt.Println(filepath.Ext("\/dir\/filename.txt")) \/\/ .txt\nfmt.Printf("%q", filepath.Ext("\/dir\/filename")) \/\/ "" <- note it is empty string\n\n<\/code><\/pre>\n
\n
os.Remove()<\/code> to delete a single file. You can also use
os.RemoveAll()<\/code> to recursively delete a directory.<\/li>\n<\/ul>\n
Unit-testing<\/h3>\n