How to split a string that is a full path, into its directories?

Say I have a string for the full path of a directory

let myPath = '/go/src/github.com/MyDomain/MyProject'


I would like to have an array like

['go', 'src', 'github.com', 'MyDomain', 'MyProject']


What is the best way of doing that ? I would prefer to make this platform agnostic. So if there is a split() function tailored for paths, I would prefer that.

Otherwise I think

let out = split(myPath, '/')


Should do the trick ?

• Be aware of the UNC pathes (\\server\share\path\to\file) on Windows.
– Ralf
Jun 8, 2020 at 6:37

I would use regular split function with regex covering 2 path separators.
Windows can use / or \, plus mix of both:
echo split('/go/src/github.com/MyDomain/MyProject', '[/\\]')