Using struct tag directives

Report a typo

Edwin is a doctor; he has created a Go program that has the Appointment struct that will hold important data about his patients' medical appointments. This way, his secretary will check and organize his schedule for the month of April.

Since his secretary should not know the private details about what Dr. Edwin will discuss with patients during the medical appointment (due to Doctor-Patient confidentiality), he wants to make sure the Description field is not encoded to JSON.

Dr. Edwin has already written the required code to take as an input the values for the Appointment struct and serialize it to JSON afterward. However, he needs your help to properly add the correct JSON struct tags to his code to follow the camelCase naming convention and use the correct directive to make the Description field never be encoded to JSON.

This problem uses the time package to calculate a specific date appointment with the time.Time struct and time.Date() functions. However, to solve this task, you don't need to know how the time package works. The only objective is to add the correct JSON struct tags along with the right directives to achieve the serialized output Dr. Edwin wants.

Below is the code Dr. Edwin has written so far:

Sample Input 1:

Carlos Heart_Transplant 2022 1 9

Sample Output 1:

{"patientName":"Carlos","startTime":"2022-04-01T09:00:00Z","endTime":"2022-04-01T10:00:00Z"}
Write a program in Go
package main

import (
"encoding/json"
"fmt"
"log"
"time"
)

// Add the correct JSON struct tags with the required optional directives to the 'Appointment' struct.
type Appointment struct {
PatientName string `?:"?"`
Description string `?:"?"`
StartTime time.Time `?:"?"`
EndTime time.Time `?:"?"`
}

// DO NOT change the code within the main function! - Your task is only to add the correct JSON struct tags above!
func main() {
// DO NOT delete! - This code block takes as an input the values for the 'appointment' struct:
var patientName, description string
var year, day, hour int
fmt.Scanln(&patientName, &description, &year, &day, &hour)
startTime := time.Date(year, time.April, day, hour, 0, 0, 0, time.UTC)
endTime := startTime.Add(time.Hour * 1)

appointment := Appointment{
PatientName: patientName,
Description: description,
StartTime: startTime,
EndTime: endTime,
}

appointmentJSON, err := json.Marshal(appointment)
if err != nil {
log.Fatal(err)
}

fmt.Println(string(appointmentJSON))
}
___

Create a free account to access the full topic