Q:
Golang program to read a structure from the file
belongs to collection: Golang File Handling Programs
Golang File Handling Programs
- Golang program to create a text file
- Golang program to write and read data from a text file
- Golang program to create an empty file
- Golang program to get the size of an existing file in bytes
- Golang program to print the permissions of the existing file
- Golang program to print the last modified time of an existing file
- Golang program to check the specified file is a directory or not
- Golang program to rename a specified file with a new name
- Golang program to delete a specified file
- Golang program to open a file in read-only mode
- Golang program to open a file in append mode
- Golang program to check a specified file is exists or not
- Golang program to set file permission
- Golang program to change the ownership of the file in the Linux operating system
- Golang program to truncate the data of the file
- Golang program to create a copy of the existing file
- Golang program to commit or sync data into memory disk
- Golang program to write data bytes into a file
- Golang program to read bytes from the file
- Golang program to demonstrate the Seek() function to random access from the file
- Golang program to write data into a file in a single line
- Golang program to read data from the file in a single line
- Golang program to get the size of the file using Seek() function
- Golang program to read data line by line from file using scanner
- Golang program to read data from file word by word using the scanner
- Golang program to create an empty zip file
- Golang program to create a zip file containing a text file
- Golang program to create a temp directory in the \'/tmp\' directory
- Golang program to create a temp directory at the specified path
- Golang program to create a temp file in the specified temp directory
- Golang program to write a structure of binary data into the file
- Golang program to read a structure from the file
- Golang program to create a specified directory with specified permission
- Golang program to create multiple directories with specified permission
- Golang program to delete an empty directory
- Golang program to list the content of the specified directory
- Golang program to read the content of a specified directory and check directories and files
Program/Source Code:
The source code to read a structure from the file is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
Output:
Explanation:
In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt, os packages then we can use a function related to the fmt and os package.
Here, we also imported the "encoding/binary" package to read and write data into binary format.
In the above code, we created a structure Str that contains integer and float numbers.
In the main() function, we open an existing file "data.bin" and read data in structure object in binary format from the file using binary.Read() function. After that, we printed the result on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer