Golang os.Lstat() function is “used to get information about a file, without following symbolic links”. It accepts the name as an argument and returns the fileinfo struct.
Syntax
func Lstat(name string) (FileInfo, error)
Parameters
name: It is the file or directory name for which you want to get the information.
Return value
The Lstat() function returns a FileInfo struct, which contains information about the file or directory, such as its size, mode, and modification time. If there is an error, the function returns an error value.
Example 1: Simple usage of os.Lstat() function
package main
import (
"fmt"
"os"
)
func main() {
fileInfo, err := os.Lstat("app.txt")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(fileInfo)
}
Output
&{app.txt 0 420 {528096986 63811822410 0x102350580}
{16777232 33188 1 36339649 501 20 0 [0 0 0 0] {1676225610 528096986}
{1676225610 528096986} {1676225610 528096986} {1676225610 528096986}
0 0 4096 0 0 0 [0 0]}}
Example 2: Checking for Symbolic Links
This example checks if a given file or directory is a symbolic link.
package main
import (
"fmt"
"log"
"os"
)
func main() {
fileName := "new.txt"
info, err := os.Lstat(fileName)
if err != nil {
log.Fatalf("Error retrieving file info: %s", err)
}
if (info.Mode() & os.ModeSymlink) != 0 {
fmt.Println(fileName, "is a symbolic link.")
} else {
fmt.Println(fileName, "is not a symbolic link.")
}
}
Output
new.txt is not a symbolic link.
That’s it!
Related posts

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.