This project is a Go application that performs basic CRUD (Create, Read, Update, Delete) operations on a JSON file named person.json. It demonstrates how to work with JSON data and file I/O in Go.
-
Data Structure:
- Defines a
Personstruct withName(string) andAge(uint) fields.
- Defines a
-
Create Data:
- Function
CreateData(a []Person):- Creates a new JSON file
person.json. - Writes an array of
Personstructs to the file in JSON format.
- Creates a new JSON file
- Function
-
Read Data:
- Function
ReadData():- Opens and reads the
person.jsonfile. - Outputs the JSON content to the console.
- Opens and reads the
- Function
-
Update Data:
- Function
UpdateData(a []Person):- Opens
person.jsonin write mode. - Overwrites the existing content with new JSON data from an array of
Personstructs.
- Opens
- Function
-
Delete Data:
- Function
DeletData():- Deletes the
person.jsonfile from the filesystem.
- Deletes the
- Function
-
Main Execution Flow:
- Creates initial data with two
Personentries and writes toperson.json. - Reads and displays the file content.
- Updates the data with a new set of
Personentries. - Reads and displays the updated file content.
- Deletes the
person.jsonfile.
- Creates initial data with two
-
Prerequisites:
- Install Go (version 1.13 or higher).
-
Setup:
- Save the provided code in a file named
main.go.
- Save the provided code in a file named
-
Running the Application:
- Open a terminal and navigate to the directory containing
main.go. - Execute the command:
go run main.go
- The application will perform the following steps:
- Create
person.jsonwith initial data. - Read and display the content of
person.json. - Update
person.jsonwith new data. - Read and display the updated content.
- Delete
person.json.
- Create
- Open a terminal and navigate to the directory containing
-
Expected Output:
- Messages indicating the success or failure of each operation.
- The content of
person.jsonbefore and after the update.
- JSON Encoding/Decoding: Utilizes Go's
encoding/jsonpackage to marshal and unmarshal data. - File I/O Operations: Uses Go's
osandiopackages for file handling. - Standard Library: No external dependencies required.