The Io/ioutil package in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Several function methods

Name Role Note
ReadAll Reads the data, returns the read byte slice 1
ReadDir Reads a directory and returns an array of directory entries []os. FileInfo, 2
ReadFile Reads a file, returns the contents of the file (byte slice) 3
WriteFile Write bytes Slice According to the file path 4
TempDir Creates a temporary directory with the specified prefix name in a directory, returning the path to the new temporary directory 5
Tempfile Creates a temporary file of the specified prefix name in a directory, returning to the OS. File 6

Example

ReadAll

func ReadAll(r io.Reader) ([]byte, error)
r := strings.NewReader("Go is a general-purpose language designed with systems programming in mind.")b, err := ioutil.ReadAll(r)if err != nil {    log.Fatal(err)}fmt.Printf("%s", b)/*output:Go is a general-purpose language designed with systems programming in mind.*/

ReadDir

func ReadDir(dirname string) ([]os.FileInfo, error)
files, err := ioutil.ReadDir(".")if err != nil {    log.Fatal(err)}for _, file := range files {    fmt.Println(file.Name())}

WriteFile

func WriteFile(filename string, data []byte, perm os.FileMode) error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.