- Struct Creation:
- Define a struct
Plane
with fields:Manufacturer
(string),Model
(string), andCapacity
(int). - Instantiate a
Plane
representing a specific model.
- Define a struct
- Method with Receiver:
- Add a method
Details
to thePlane
struct that prints the plane’s manufacturer, model, and capacity.
- Add a method
- Pointer Receiver:
- Add a method
UpdateCapacity
to thePlane
struct that takes a new capacity as an argument and updates theCapacity
field. Use a pointer receiver to make the changes persist.
- Add a method
- Interface:
- Define an interface
Aircraft
with the methodDetails
. - Ensure that the
Plane
struct implements this interface.
- Define an interface
- Empty Interface:
- Write a function
DescribeAircraft
that takes an argument of typeinterface{}
and prints out the type and value of the argument. - Test this function by passing a
Plane
instance and other types.
- Write a function
- Putting it All Together:
- Create a slice of
Aircraft
and add several differentPlane
instances to it. - Iterate over the slice, calling the
Details
method for each aircraft. - Use the
DescribeAircraft
function 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