Golang is one of the more popular programming languages today. It also provides many convenient methods and tool functions for file operations. In this article, we will introduce several common file operation methods in golang.
Creating a file is the first step in file operation. Golang provides an os library to complete the file creation operation. We can create new files using the os.Create() function.
For example, the following code can create a new file named test.txt:
package main import ( "fmt" "os" ) func main() { file, err := os.Create("test.txt") if err != nil { fmt.Println(err) return } defer file.Close() fmt.Println("New file created successfully") }
The os.Create("test.txt") function will create a new file named test.txt in the current directory. test.txt file. If the err returned is not nil, the file creation was unsuccessful and we need to handle the error and terminate the program.
defer file.Close() statement causes the file to be automatically closed when the function returns.
When the file is created successfully, we need to write some data to the file. Golang provides the bufio library to complete the file writing operation.
For example, the following code can write Hello world in the newly created test.txt file:
package main import ( "bufio" "fmt" "os" ) func main() { file, err := os.Create("test.txt") if err != nil { fmt.Println(err) return } defer file.Close() writer := bufio.NewWriter(file) _, err = writer.WriteString("Hello world") if err != nil { fmt.Println(err) return } writer.Flush() fmt.Println("Data written successfully") }
bufio.NewWriter(file) creates a new writer to write to the file data. The writer.WriteString("Hello world") function writes data to the buffer, and the writer.Flush() function writes the buffer contents to the file.
In addition to writing files, we also need to read the contents of files. Golang also provides the bufio library to complete file reading. We can use the bufio.NewReader() function to read files.
For example, the following code can read the contents of the test.txt file just written:
package main import ( "bufio" "fmt" "os" ) func main() { file, err := os.Open("test.txt") if err != nil { fmt.Println(err) return } defer file.Close() reader := bufio.NewReader(file) text, err := reader.ReadString(' ') if err != nil { fmt.Println(err) return } fmt.Println(text) }
os.Open("test.txt") function opens the test.txt file, The bufio.NewReader(file) function reads the file content into the memory buffer, and the reader.ReadString('
') function reads the content in the buffer into the text variable. Here we use '
' as the delimiter and stop reading after reading '
'.
After completing the file operation, we may need to delete the file. golang provides the os.Remove() function to let us delete files.
For example, the following code can delete the test.txt file just created, written, and read:
package main import ( "fmt" "os" ) func main() { err := os.Remove("test.txt") if err != nil { fmt.Println(err) return } fmt.Println("File deleted successfully") }
os.Remove("test.txt") function will delete test.txt The file is removed from the file system. If the remove operation is unsuccessful, err will not be nil.
Summary
In this article, we introduced several commonly used file operation methods in golang, including creating files, writing files, reading files, and deleting files. These operations are very simple and convenient in golang, especially using the bufio library to handle file operations, which can greatly improve the efficiency and speed of file writing and reading.
The above is the detailed content of Several file operations in golang. For more information, please follow other related articles on the PHP Chinese website!