Home  >  Article  >  Backend Development  >  How to delete files in go language

How to delete files in go language

藏色散人
藏色散人Original
2021-01-18 14:39:2617773browse

How to delete files in Go language: 1. Determine the path of the source file that needs to be deleted; 2. Delete the specified file through "os.Remove(file)"; 3. Through "if err != nil{.. .}else{...}" method to determine whether the file is deleted successfully.

How to delete files in go language

The operating environment of this article: Windows 7 system, Go1.11.2 version, Dell G3 computer.

Go comes with the Remove() function in the OS package to delete files.

/* 删除文件函数
   os.Remove(file string) error
   file  文件名
   error 如果失败则返回错误信息
*/
package main
import (
    "os"
    "fmt"
)
func main() {
    file := "test.txt"                   //源文件路径
    err := os.Remove(file)               //删除文件test.txt
    if err != nil {
        //如果删除失败则输出 file remove Error!
        fmt.Println("file remove Error!")
        //输出错误详细信息
        fmt.Printf("%s", err)
    } else {
        //如果删除成功则输出 file remove OK!
        fmt.Print("file remove OK!")
    }
}
//该片段来自于http://outofmemory.cn

Related introduction:

Go (also known as Golang) is a statically strongly typed, compiled, concurrent programming language with garbage collection capabilities developed by Google.

Robert Griesemer, Rob Pike and Ken Thompson began designing Go in September 2007, and later Ian Lance Taylor, Russ Cox joins the project. Go is developed based on the Inferno operating system. Go was officially announced in November 2009, becoming an open source project and implemented on Linux and Mac OS X platforms, and later added implementation under Windows systems. In 2016, Go was selected as "TIOBE's Best Language of 2016" by the software evaluation company TIOBE. Currently, Go releases a second-level version every six months (that is, upgrading from a.x to a.y).

For more go language technical articles, please visit the go language tutorial column!

The above is the detailed content of How to delete files in go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn