TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang ioutil.WriteFile, os.Create (Write File to Disk)

Write files to disk with ioutil.WriteFile and os.Create. Use strings and byte slices.
WriteFile. A file can be written to disk. With Golang we can use a helper module like ioutil to do this easily. For other cases, a NewWriter can be used to write many parts.
With ioutil, we can avoid calling os.Create and bufio.NewWriter. The ioutil.WriteFile method simplifies writing an entire file in one call.
Ioutil example. To begin, we have a string we want to write to a file ("Hello friend"). Then we convert this string to a byte slice so we can pass it to WriteFile.

WriteFile: This method receives the target file's path, the bytes we wish to write, and a flag value (zero works fine for testing).

Output: The string "Hello friend" is written to the file correctly. The permissions (in Linux) may need to be adjusted first.

Golang program that uses ioutil.WriteFile package main import ( "fmt" "io/ioutil" ) func main() { // Get byte data to write to file. dataString := "Hello friend" dataBytes := []byte(dataString) // Use WriteFile to create a file with byte data. ioutil.WriteFile("/home/sam/example.txt", dataBytes, 0) fmt.Println("DONE") } Output Hello friend
Os.create, bufio. We create a new Writer with the bufio.NewWriter function. The file handle we pass to NewWriter must have write access. We must use os.Create (not os.Open) to write a file.

Here: We write the string "ABC" to a file on the disk. A new file is created if none exists.

Flush: We use flush after writing to the file to ensure all the writes are executed before the program terminates.

Golang program that writes strings to file package main import ( "bufio" "os" ) func main() { // Use os.Create to create a file for writing. f, _ := os.Create("C:\\programs\\file.txt") // Create a new writer. w := bufio.NewWriter(f) // Write a string to the file. w.WriteString("ABC") // Flush. w.Flush() } Results: file.txt ABC
A summary. With Golang we have a language that is well-optimized for writing files to disk. The ioutil module, as always, can reduce the amount of code needed to perform this common task.File
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf