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.Join() en el lenguaje Go solía unir cualquier número de elementos de ruta especificados en una sola ruta, agregando un Separador si era necesario. Esta función llama a Limpiar en el resultado y se ignoran todas las strings vacías. 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 Join(elem ...string) string
Aquí, ‘elem’ son los elementos de ruta especificados.
Valor devuelto: Devuelve la ruta única unida.
Ejemplo 1:
// Golang program to illustrate the usage of // filepath.Join() function // Including the main package package main // Importing fmt and path/filepath import ( "fmt" "path/filepath" ) // Calling main func main() { // Calling the Join() function fmt.Println(filepath.Join("G", "F", "G")) fmt.Println(filepath.Join("G/F", "G")) fmt.Println(filepath.Join("gfg", "GFG")) fmt.Println(filepath.Join("Geeks", "for", "Geeks")) }
Producción:
G/F/G G/F/G gfg/GFG Geeks/for/Geeks
Ejemplo 2:
// Golang program to illustrate the usage of // filepath.Join() function // Including the main package package main // Importing fmt and path/filepath import ( "fmt" "path/filepath" ) // Calling main func main() { // Calling the Join() function fmt.Println(filepath.Join("/", "/")) fmt.Println(filepath.Join("")) fmt.Println(filepath.Join("a/b", "/c")) fmt.Println(filepath.Join("5", "10")) fmt.Println(filepath.Join(".")) }
Producción:
/ a/b/c 5/10 .
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