- Struct Creation:
- Define a struct
Planewith fields:Manufacturer(string),Model(string), andCapacity(int). - Instantiate a
Planerepresenting a specific model.
- Define a struct
- Method with Receiver:
- Add a method
Detailsto thePlanestruct that prints the plane’s manufacturer, model, and capacity.
- Add a method
- Pointer Receiver:
- Add a method
UpdateCapacityto thePlanestruct that takes a new capacity as an argument and updates theCapacityfield. Use a pointer receiver to make the changes persist.
- Add a method
- Interface:
- Define an interface
Aircraftwith the methodDetails. - Ensure that the
Planestruct implements this interface.
- Define an interface
- Empty Interface:
- Write a function
DescribeAircraftthat takes an argument of typeinterface{}and prints out the type and value of the argument. - Test this function by passing a
Planeinstance and other types.
- Write a function
- Putting it All Together:
- Create a slice of
Aircraftand add several differentPlaneinstances to it. - Iterate over the slice, calling the
Detailsmethod for each aircraft. - Use the
DescribeAircraftfunction to print detailed information about each aircraft.
- Create a slice of
![Go Lab [4] – struct, method, pointer receiver , receiver , interface, empty interface](https://blog.jumpbox.academy/wp-content/uploads/2024/01/go-1024x700.png)
Posted inGO Lang