Skip to content
Breaking
Go Lab [5] – Do it for your self
Go Lab [3] – type ,Pointer ,zero value, array, slice ,variadic function, map
Go Lab [4] – struct, method, pointer receiver , receiver , interface, empty interface
Go Lab [2] – condition, loop, testing
Go Lab [1] – variable, function,constants, number, arithmetic operators
Temp in The City
- Type Declaration:
- Define a new type
Temperature
which is an alias for float64
.
- Create a variable
roomTemp
of type Temperature
and initialize it to a suitable value.
- Pointer:
- Write a function
increaseTemp
that takes a pointer to Temperature
and increases its value by a given amount.
- Zero Value:
- Declare a variable
defaultTemp
of type Temperature
without initializing it. Print its value to demonstrate the zero value of a float64
.
- Array:
- Create an array
weekTemps
of type Temperature
with a length of 7, representing temperatures for a week.
- Slice:
- Create a slice
weekendTemps
from the weekTemps
array that includes only temperatures for the weekend.
- Variadic Function:
- Write a function
averageTemp
that calculates the average temperature. It should take a variadic Temperature
slice as an argument.
- Map:
- Create a map
tempByCity
where keys are city names (as string
) and values are Temperature
.
- Add at least three city-temperature pairs to the map, and then iterate over the map to print each city and its corresponding temperature.
- Putting it All Together:
- Use the
increaseTemp
function to increase the temperature of a city in tempByCity
.
- Update the
weekTemps
array based on new data and recalculate the average using averageTemp
.