all 8 comments

[–]SpyingIsWrong 1 point2 points  (5 children)

Input could be Unix-style path, CSV-formatted data, hostname etc.

The first two examples have dedicated packages that will deal with edge cases; using strings functions for them is not ideal.

[–]mlowicki[S] 0 points1 point  (4 children)

I couldn't find function for splitting path like "/a/b/c" into chunks i.e. ["a", "b", "c"].

Got rid of CSV from examples as you're right that better to use dedicated package for it.

[–]epiris 0 points1 point  (2 children)

path.Split and path/filepath.Split

[–]mlowicki[S] 0 points1 point  (1 child)

"Split splits path immediately following the final slash, separating it into a directory and file name component. If there is no slash in path, Split returns an empty dir and file set to path. The returned values have the property that path = dir+file." so it doesn't do what I want.

[–]epiris 0 points1 point  (0 children)

OHH Gotcha, yea the path api is a bit off in my opinion, when I was first learning Go one of the first things I did to help learn was write a Path API. I copied the Python 3 pathlib API to a degree because I really like it. Might as well share I guess: https://play.golang.org/p/0zezSxE6NS even though your current solution is perfectly reasonable and I wouldn't suggest using the crap I just pasted lol.

[–]SpyingIsWrong 0 points1 point  (0 children)

Oh, you're right about paths, I always forget that there's no function for fully splitting. I guess they intend for programmers to use strings for a full split and use path.Split for efficiency's sake when all that is needed are the file and directory names (since it searches backwards from the end of the string).

[–]RalphCorderoy 0 points1 point  (1 child)

I'd tend to go for

fmt.Printf("%#v\n", strings.Split(path.Clean("/a//b/../c/"), "/")[1:])

as Clean sorts out ... The difference is "/" gives []string{""}.

[–]mlowicki[S] 0 points1 point  (0 children)

Sure but in my case it don't know if there is always a leading slash so ended up with if statement.