- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to write into a file
In go programming language we can use os.create and ioutil.WriteFile to write into a file. In Go, the OS can be used to represent a file. os package file type that offers ways to open, read from, write to, and manipulate files.
Method 1: Using os.Create function
We make use of the os.Create in this program to start a new file or, if one already exists, to open it. The file is deferred while a string is written using the WriteString function. Close makes ensuring that when the application ends, the file is correctly closed.
Syntax
Os.Create
In Go programming language, create is a part of os package , this function creates a new file, it contains a single parameter i.e. the filename which is to be created.
Algorithm
Step 1 − Create a package main and declare fmt(format package) and os package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 − Use the os.Create in the main to open the file for writing.
Step 3 − Check for any potential file-creation issues, such as the file not being located.
Step 4 − After all activities are finished, use the defer keyword to terminate the file.
Step 5 − Employ the file to write a string to a file, use the WriteString method.
Step 6 − Verify your text for any mistakes that might appear, such as insufficient permissions.
Example
In this example we will use os.Create function to write into a file. Let’s see how the code is executed.
package main import ( "fmt" "os" ) //create main function to execute the program func main() { // Open the file for writing file, errs := os.Create("myfile.txt") if errs != nil { fmt.Println("Failed to create file:", errs) return } defer file.Close() // Write the string "Hello, World!" to the file _, errs = file.WriteString("Hello, World!") if errs != nil { fmt.Println("Failed to write to file:", errs) //print the failed message return } fmt.Println("Wrote to file 'myfile.txt'.") //print the success message }
Output
Wrote to file 'myfile.txt'.
Method 2: Using ioutil.WriteFile
In this program we write a string to a file using the ioutil.WriteFile function. The file name is the first argument, the data to be written as a byte slice is the second argument, and the file permission is the third argument. If something goes wrong, the function returns an error, which we check for throughout the program.
Syntax
ioutil.WriteFile
In Go, WriteFile belongs to ioutil package and contains three parameters, the first is the filename where data is to be written, second is the data which is to be written and third is the file permission. The data is written to the file if function is executed successfully.
Algorithm
Step 1 − Create a package main and declare fmt(format package) and io/ioutil package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 − Use the ioutil.WriteFile in the main function to write a string to a file.
Step 3 − The file's name is the first argument and the data that will be written as a byte slice is the second argument. Here, the third argument is the file permission.
Step 4 − Verify your text for any mistakes that might appear, such as insufficient permissions.
Step 5 − Print a message stating that the file was successfully written to if there were no issues.
Step 6 − The print statement is executed using fmt.Println() function
Example
In this example, we will use ioutil.WriteFile method to write into a file. Let’s see via code how it can be executed.
package main import ( "fmt" "io/ioutil" ) //create main function to execute the program func main() { // Write the string to the file err := ioutil.WriteFile("myfile.txt", []byte("Hello, alexa!"), 0644) if err != nil { fmt.Println("Failed to write to file:", err) //print the failed message return } fmt.Println("Wrote to the file 'myfile.txt'.") //print the success message }
Output
Wrote to the file 'myfile.txt'.
Conclusion
We executed the program of writing into a file using two methods. In the first method we used os.Create function and in the second example we used ioutil.WriteFile command to execute the program. If the content is written to the file success message is printed but if it cannot write to file , failed message will be printed on the console.
- Related Articles
- Golang program to copy one file into another file
- Golang Program to open a file in write-only mode
- Golang Program to open a file in read-write mode with truncating file
- Golang Program to open a file in the read-write mode without truncating the file
- Golang program to delete a file
- Golang program to rename a file
- How to read a file into a string in Golang?
- Golang program to create a new file
- Golang program to create a temporary file
- Write a Python program to export dataframe into an Excel file with multiple sheets
- Golang program to make a file read-only
- Golang Program to check if a file is directory or a file
- Write a Golang program to reverse a string
- Write a Golang program to reverse a number
- Golang Program to Read the Contents of a File
