Golang实现获取文件的后缀名(扩展名)
Golang通过调用path.Ext()
函数,可获取文件的后缀名。
代码实现如下:
package main
import (
"path"
"fmt"
)
func main(){
filepath := "C:\\Users\\Administrator\\Desktop\\user_agents.py"
fileExt := path.Ext(filepath)
fmt.Println(fileExt)
}
通过这个方法来判断文件的后缀名很方便;
比如判断某个文件是否为.bmp
格式:
package main
import (
"path"
"fmt"
)
func main(){
filepath := "C:\\Users\\Administrator\\Desktop\\user_agents.py"
fileExt := path.Ext(filepath)
if fileExt != ".bmp"{
fmt.Println("file type must be bmp")
}
// ...
}