Go Lab [3] – type ,Pointer ,zero value, array, slice ,variadic function, map

Temp in The City

  1. 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.
  2. Pointer:
    • Write a function increaseTemp that takes a pointer to Temperature and increases its value by a given amount.
  3. Zero Value:
    • Declare a variable defaultTemp of type Temperature without initializing it. Print its value to demonstrate the zero value of a float64.
  4. Array:
    • Create an array weekTemps of type Temperature with a length of 7, representing temperatures for a week.
  5. Slice:
    • Create a slice weekendTemps from the weekTemps array that includes only temperatures for the weekend.
  6. Variadic Function:
    • Write a function averageTemp that calculates the average temperature. It should take a variadic Temperature slice as an argument.
  7. 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.
  8. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *