función filepath.IsAbs() en Golang con ejemplos

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.IsAbs() en el lenguaje Go solía informar si la ruta es absoluta o no. 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 IsAbs(path string) bool

Aquí, ‘ruta’ es la ruta absoluta o no absoluta especificada.

Valor devuelto: Devuelve verdadero para la ruta absoluta; de lo contrario, devuelve falso.

Ejemplo 1:

// Golang program to illustrate the usage of
// filepath.IsAbs() function
  
// Including the main package
package main
  
// Importing fmt and path/filepath
import (
    "fmt"
    "path/filepath"
)
  
// Calling main
func main() {
  
    // Calling the IsAbs() function with
    // some absolute and unabsolute paths
    fmt.Println(filepath.IsAbs("/home/gfg"))
    fmt.Println(filepath.IsAbs(".gfg"))
    fmt.Println(filepath.IsAbs("/gfg"))
    fmt.Println(filepath.IsAbs(":gfg"))
}

Producción:

true
false
true
false

Ejemplo 2:

// Golang program to illustrate the usage of
// filepath.IsAbs() function
  
// Including the main package
package main
  
// Importing fmt and path/filepath
import (
    "fmt"
    "path/filepath"
)
  
// Calling main
func main() {
  
    // Calling the IsAbs() function with
    // some absolute and unabsolute paths
    fmt.Println(filepath.IsAbs("/"))
    fmt.Println(filepath.IsAbs("."))
    fmt.Println(filepath.IsAbs(":"))
    fmt.Println(filepath.IsAbs("/."))
    fmt.Println(filepath.IsAbs("//"))
    fmt.Println(filepath.IsAbs(":/"))
}

Producción:

true
false
false
true
true
false

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

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *