|

How to test a file or directory exists in Go?

How to test a path (a file or directory exists) in golang?

In Bash, the [ -e ] tests can test whether a file or a directory exist. What are the corresponding Go ways for the test?


You can use the `os.Stat()` and `os.IsNotExist()` standard libraries functions together to test whether a file or a directory exists in Go.

if _, err := os.Stat("/tmp/aaaa"); err != nil {
    if os.IsNotExist(err) {
        // file does not exists
    } else {
        // file exists
    }
}

Similar Posts

  • Proof of Stake (PoS) 101

    Blockchain technology is based on the idea of decentralization, transparency, and security. One of the key challenges in blockchain technology is reaching consensus on the state of the ledger. Consensus mechanisms ensure that all nodes in the network agree on the current state of the blockchain. Proof of Stake (PoS) is a consensus mechanism that…

  • Managing LVM

    Any tutorials or tips on how to manage LVM? LVM provides a flexible and easy way to management storage disks on Linux. It also provides a set of tools for management. Here are some tutorials and references for managing LVM: LVM man pages: https://www.systutorials.com/docs/linux/man/8-lvm/#lbAL Manage Disk Volume in Linux: http://www.hongkedavid.com/blog/disk_volume.html LVM with Xen: https://www.systutorials.com/b/linux/tag/lvm/ Remove missing phyiscal volumes…

  • GDB Cheat Sheet

    Here is a collection of good GDB Cheat Sheets from the Internet. GDB QUICK REFERENCE GDB Version 5 GDB Cheat Sheet gdb Cheatsheet GDB commands by function – simple guide Read more: Latex Cheat Sheet gdb like step by step debugger for Python Vim + cgdb Notes for Beginners of Software Development on Linux