Golang basic knowledge of file operations

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

Read all the contents of the file and get the file action object

Package Mainimport ("Bufio" "FMT" "io" "io/ioutil" "OS") func check (e error) {if E! = nil {panic (e)}}func main () {dat, err: = i Outil. ReadFile ("/tmp/dat") Check (err) fmt. Print (string (DAT)) F, err: = OS. Open ("/tmp/dat") Check (ERR) defer f.close ()}

File Write operations

Package Mainimport ("Io/ioutil" "OS") func check (e error) {if E! = nil {panic (e)}}func main () {d1: = []byte ("hello\ngo\n") er r: = Ioutil. WriteFile ("/tmp/dat1", D1, 0644) Check (err) F, err: = OS. Create ("/tmp/dat2") Check (ERR) defer f.close ()}

In fact there is no major operating code, the specific use of a more simple packaging class.

Permission mode of the file Cheetsheet

Note

This series is not a chatty Package API guide, but includes only the most common uses that the authors consider most commonly used. The protests were ineffective.

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.