In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the Go language how to operate on the file of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Go language on how to operate the document article will have a harvest, let's take a look at it.
# create and open files
/ / New files can be created in the following two ways:
Func Create (name string) (file * File, err Error)
Create a new file based on the file name provided and return a file object with a default permission of 0666. The returned file object is readable and writable.
Func NewFile (fd uintptr, name string) * File
Create the corresponding file according to the file descriptor and return a file object
/ / Open the file in the following two ways:
Func Open (name string) (file * File, err Error)
This method opens a file named name, but in a read-only manner, the internal implementation actually calls OpenFile.
Func OpenFile (name string, flag int, perm uint32) (file * File, err Error)
Open the file named name. Flag is the way to open it, read-only, read-write, etc. Perm is the permission
/ / write a file
Func (file * File) Write (b [] byte) (n int, err Error)
Write information of type byte to a file
Func (file * File) WriteAt (b [] byte, off int64) (n int, err Error)
Start writing information of byte type at the specified location
Func (file * File) WriteString (s string) (ret int, err Error)
Write string information to a file
/ / read the file
Func (file * File) Read (b [] byte) (n int, err Error)
Read data into b
Func (file * File) ReadAt (b [] byte, off int64) (n int, err Error)
Start reading data from off into b
/ / Delete files
Func Remove (name string) Error
You can delete the file named name by calling this function.
/ / close the file
Func (file * File) Close ()
Write a file
/ / code_034_os_write_to_file project main.gopackage mainimport ("fmt"os") func main () {/ / New file fout, err: = os.Create (". / createfile.txt") if err! = nil {fmt.Println (err) return} defer fout.Close () for I: = 0; I < 5 ITunes + {/ / remarks: in windows environment, the end\ r\ ncan be wrapped, and under linux, outstr: = fmt.Sprintf ("% slug% d\ r\ n", "Hello Go", I) / / Sprintf console output And the return value string / / writes the file fout.WriteString (outstr) / / string information fout.Write ([] byte ("abcd\ r\ n")) / / byte type}}
Read the file
/ / code_035_os_read_from_file project main.gopackage mainimport ("fmt"os") func main () {fin, err: = os.Open (". / open_and_read1.txt") if err! = nil {fmt.Println (err) / / if the file does not exist: The system cannot find the file specified. } defer fin.Close () buf: = make ([] byte, 1024) / / create storage slice for {n, _: = fin.Read (buf) / / read file if n = = 0 {break} fmt.Println (string (buf))}}
Copy file-"(Note: local_copy_file.txt that has been created and written) the terminal changes to the current directory and executes go run main.go local_copy_file.txt dst_file.txt
/ / code_036_os_copy_file project main.gopackage mainimport ("fmt"io"os") func main () {/ / use command line to improve the reusability of copies args: = os.Args if args = = nil | | len (args)! = 3 {fmt.Println ("useage: go filename.go src File dstFile") return} srcPath: = args [1] dstPath: = args [2] fmt.Printf ("srcPath =% s") DstPath =% s\ r\ n ", srcPath, dstPath) if srcPath = = dstPath {fmt.Println (" source and destination files cannot be renamed ")} / / copy srcFile, err1: = os.Open (srcPath) if err1! = nil {fmt.Println (err1) return} dstFile, err2: = os.Create (dstPath) if err2! = nil {fmt.Println (err2) return} read_buf: = make ([] byte) 1024) for {/ / read file n Err: = srcFile.Read (read_buf) / / length of bytes per file read if err! = nil & & err! = io.EOF {fmt.Println (err) break} if n = 0 {fmt.Println ("file processed") break} / / write destination file write_buf: = read_buf [: n] dstFile.Write (write_buf )} / / close the file srcFile.Close () dstFile.Close ()} this is the end of the article on "how the Go language operates on files" Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to manipulate documents in Go language". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.