The problem of Go getting a file compound extension (such as tar.bz2)

for example, if the file name is test.tar.bz2, you can only get the bz2 extension through the path.Ext method. How can you get the tar.bz2 extension without knowing in advance whether it is a single extension or a compound extension?

Jan.18,2022

if the symbol . is not included in the root file name at all:

divide the file name according to . , and then merge the parts except the file name ~

func ext(path string) string {
    var fileExt string
    for i := len(path) - 1; i >= 0; i-- {
        if path[i] == '/' {
            break
        }
        if path[i] == '.' {
            fileExt = path[i:]
        }
    }
    return fileExt
}

of course, you can also combine string processing functions such as path.Split () and strings.Index () .

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb5fea-2593.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb5fea-2593.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?