En el lenguaje Go, el paquete de ruta se usa para rutas separadas por barras inclinadas, como las rutas en las URL. La función filepath.Base() en el lenguaje Go solía devolver el último elemento de la ruta especificada. Aquí se eliminan los separadores de ruta de seguimiento antes de extraer el último elemento. Si la ruta está vacía, Base devuelve “.”. Si la ruta consta completamente de separadores, Base devuelve un solo separador. Además, esta función se define en el paquete de ruta. Aquí, debe importar el paquete «ruta/ruta de archivo» para usar estas funciones.
Sintaxis:
func Base(path string) string
Aquí, ‘ruta’ es la ruta especificada.
Valor devuelto: Devuelve el último elemento de la ruta especificada. Si la ruta está vacía, esta función devuelve “.”. Si la ruta consta completamente de separadores, esta función devuelve un único separador.
Ejemplo 1:
// Golang program to illustrate the usage of // filepath.Base() function // Including the main package package main // Importing fmt and path/filepath import ( "fmt" "path/filepath" ) // Calling main func main() { // Calling the Base() function to // get the last element of path fmt.Println(filepath.Base("/home/gfg")) fmt.Println(filepath.Base(".gfg")) fmt.Println(filepath.Base("/gfg")) fmt.Println(filepath.Base(":gfg/GFG")) }
Producción:
gfg .gfg gfg GFG
Ejemplo 2:
// Golang program to illustrate the usage of // filepath.Base() function // Including the main package package main // Importing fmt and path/filepath import ( "fmt" "path/filepath" ) // Calling main func main() { // Calling the Base() function which // returns "." if the path is empty // and returns a single separator if // the path consists entirely of separators fmt.Println(filepath.Base("")) fmt.Println(filepath.Base(".")) fmt.Println(filepath.Base("/")) fmt.Println(filepath.Base("/.")) fmt.Println(filepath.Base("//")) fmt.Println(filepath.Base(":/")) }
Producción:
. . / . / :
Publicación traducida automáticamente
Artículo escrito por Kanchan_Ray y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA