The GO program in this repository is basically the same as the one in struct_method_mutation_example demonstrates the use of struct and struct methods in the form of a separated package. The struct and its method needed to be imported from a package of its own.
The program features the use of pointers with structs and struct methods to display and overwrite/clear (mutate) the data in the original struct without unnecessarily occupying extra memory spaces. The features to display/clear data were written in a way that they are methods of the struct created in this program.
When run, the program will present the user with 4 choices which include Input data, Display data, Clear data, and Exit. The user can choose one of these choices by inputing a number preceeding each choice. The program validates the input at this step and will keep asking the user for input until it gets a valid input. The following choices work according to the following descriptions:
-
Input data: The user will be asked to input a student's first name, last name, (string) and score (floating number). There will not be a data validation process in this step. The score will be 0 by default if the input is not a floating number. The program keeps the input information at this step in a struct.
-
Display data: The program reads the information stored in the created struct and display it accordingly. If the user skips #1 and perform this task once the program is run, the default information for each data type will be displayed ("" for string and 0 for floating number).
-
Clear data: The program clear data in the created struct. The score which is of the floating number type will be reset to 0.
-
The program exits with a Goodbye message.
Except for the choice #4, the program will ask if the user wants to continue to perform another tasks (any among the 4 choices) after finishing each task. The user will confirm this question by inputting "Y" or "N" (case-insensitive). The program will continue to work until the user input "N" or "n".
The program is divided into 2 main parts: the main program codes in the main.go file and the struct and its methods in the student.go package file. The code in the main.go file also divided further into 2 main parts: the main program code and basic functions that displays the program menu and a helper function to control the flow of the program after each chosen choice of operation.
The struct methods in the student package include a function to take user input and store the information in the created struct, and methods to display and clear the stored infromation from the struct.
Each function and method will be called and executed according to the chosen choice. Once the user completes inputting information, the struct method that displays and clears the information will get the struct pointer as their argument and read/modify the original struct (created by the function that gets user input) directly to prevent unnecessary momory usage.
The program flow is shown as the following numbered list:
-
The program presents the user with 4 choices:
1.1 Input Data
1.2 Display Data
1.3 Clear Data
1.4 Exit
-
The user can choose any of each choice by inputting the preceeding number. There is no order for choosing any choice. But if the user chooses to display data first, the default data for strig ("") and floating number (0) types will be displayed. If the user chooses to input the data, the user inputs will be stored in a struct with the field names "first name", "last name", and "score". If the user chooses to display or clear the data, the original struct will be read/modify directly according to the use of pointer with struct data type.
-
After each choice, the program will ask if the user wants to perform another operation. The user can also exit the program at this step by inputting "N" or "n"
-
If the user chooses to exit, the program will exit with a goodbye message.