On Wednesday, April 18, 2012 6:08:43 AM UTC-4, dlin wrote:for file name as a/b.c, I want to extract b.Here is my stupid code, is there any better method?import "path"inFName := "a/b.c"fName := path.Base(inFName)extName := path.Ext(inFName)bName := fName[:len(fName)-len(extName)]//bName will be "b"
Perhaps something like this?import ("path""strings")str := "your/path.here"i, j := strings.LastIndex(str, "/"), strings.LastIndex(str, path.Ext(str))name := str[i:j]
Split the pathname path into a pair (root, ext) such that root + ext == path, and ext is empty or begins with a period and contains at most one period. Leading periods on the basename are ignored; splitext('.cshrc') returns('.cshrc', '').
Looks reasonable to me. Put it in a function if you need to call it a lot.
- Well, I just wonder, if Go provide a function like python: