获取文件扩展名

package main

import (
   "fmt"
   "path/filepath"
)

func main() {
   fileFullPath := "/dir1/dir2/dir3/demo.png"

   fmt.Printf("fileFullPath => %+v \n", fileFullPath)
   // fileFullPath => /dir1/dir2/dir3/demo.png

   // 获取文件所在目录
   fmt.Printf("filepath.Dir(fileFullPath) => %+v \n", filepath.Dir(fileFullPath))
   // filepath.Dir(fileFullPath) => /dir1/dir2/dir3

   // 获取文件名
   fmt.Printf("filepath.Base(fileFullPath) => %+v \n", filepath.Base(fileFullPath))
   // filepath.Base(fileFullPath) => demo.png

   // 获取文件扩展名(含小数点)
   fmt.Printf("filepath.Ext(fileFullPath) => %+v \n", filepath.Ext(fileFullPath))
   // filepath.Ext(fileFullPath) => .png
}

Copyright © 2024 码农人生. All Rights Reserved