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
Person
struct withName
(string) andAge
(uint) fields.
- Defines a
-
Create Data:
- Function
CreateData(a []Person)
:- Creates a new JSON file
person.json
. - Writes an array of
Person
structs to the file in JSON format.
- Creates a new JSON file
- Function
-
Read Data:
- Function
ReadData()
:- Opens and reads the
person.json
file. - Outputs the JSON content to the console.
- Opens and reads the
- Function
-
Update Data:
- Function
UpdateData(a []Person)
:- Opens
person.json
in write mode. - Overwrites the existing content with new JSON data from an array of
Person
structs.
- Opens
- Function
-
Delete Data:
- Function
DeletData()
:- Deletes the
person.json
file from the filesystem.
- Deletes the
- Function
-
Main Execution Flow:
- Creates initial data with two
Person
entries and writes toperson.json
. - Reads and displays the file content.
- Updates the data with a new set of
Person
entries. - Reads and displays the updated file content.
- Deletes the
person.json
file.
- 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.json
with initial data. - Read and display the content of
person.json
. - Update
person.json
with 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.json
before and after the update.
- JSON Encoding/Decoding: Utilizes Go's
encoding/json
package to marshal and unmarshal data. - File I/O Operations: Uses Go's
os
andio
packages for file handling. - Standard Library: No external dependencies required.