Go JSON Marshaling: Employee Data Serialization
This challenge focuses on serializing Go data structures into JSON format, a fundamental task in modern application development for data exchange and storage. You will implement a function that takes a Go struct representing employee data and converts it into a JSON string. This skill is crucial for building APIs, configuring applications, and interacting with various web services.
Problem Description
Your task is to create a Go function named MarshalEmployee that takes a pointer to an Employee struct and returns its JSON representation as a byte slice. The Employee struct contains fields for ID, Name, Department, and IsFullTime.
Key Requirements:
- Define a Go struct named
Employeewith the following fields:ID: an integer representing the employee's unique identifier.Name: a string representing the employee's full name.Department: a string representing the employee's department.IsFullTime: a boolean indicating whether the employee is full-time.
- Implement the
MarshalEmployeefunction:- It should accept
*Employeeas its input parameter. - It should return a
[]bytecontaining the JSON representation of the inputEmployeestruct. - The JSON keys should correspond to the struct field names, but with the first letter capitalized (e.g.,
IDbecomes"ID",Namebecomes"Name"). - If the input
Employeepointer isnil, the function should return an empty byte slice ([]byte{}).
- It should accept
- You should leverage Go's built-in
encoding/jsonpackage for this task.
Expected Behavior:
The function should correctly serialize the provided Employee data into a valid JSON string.
Edge Cases to Consider:
- Handling a
nilpointer for theEmployeestruct.
Examples
Example 1:
Input: &Employee{ID: 101, Name: "Alice Smith", Department: "Engineering", IsFullTime: true}
Output: []byte(`{"ID":101,"Name":"Alice Smith","Department":"Engineering","IsFullTime":true}`)
Explanation: The Employee struct is serialized into a JSON byte slice. The keys in the JSON match the capitalized struct field names.
Example 2:
Input: &Employee{ID: 205, Name: "Bob Johnson", Department: "Marketing", IsFullTime: false}
Output: []byte(`{"ID":205,"Name":"Bob Johnson","Department":"Marketing","IsFullTime":false}`)
Explanation: Another example demonstrating the serialization of a different employee.
Example 3:
Input: nil
Output: []byte{}
Explanation: When a nil Employee pointer is provided, an empty byte slice is returned.
Constraints
- The
Employeestruct will not contain nested complex data structures (e.g., slices of structs, maps of slices) for this specific challenge. - The number of fields in the
Employeestruct will not exceed 10. - The length of strings (Name, Department) will not exceed 255 characters.
- Performance is not a critical concern for this basic marshaling task.
Notes
- Go's
encoding/jsonpackage provides aMarshalfunction that is perfect for this task. - Pay attention to the exact naming of the JSON keys. The default behavior of
json.Marshalshould align with the requirement if you define your struct fields correctly. - Consider how to handle the
nilinput case explicitly within your function.