Upgrade to Pro — share decks privately, control downloads, hide ads and more …

淺談 Golang v1.21 new function min max clear

Avatar for Fionn Kuo Fionn Kuo
December 18, 2023
250

淺談 Golang v1.21 new function min max clear

Avatar for Fionn Kuo

Fionn Kuo

December 18, 2023
Tweet

Transcript

  1. Fionn Kuo • [email protected] • Fionn [email protected] Contact: • Previously

    worked as a Site Reliability Engineer at CMoney. • Gopher • Passionate about So ware engineering, DevOps, and SRE culture
  2. Max Min Function Prior to the built-in min and max

    functions Why is it designed this way Clear Function Prior to the built-in clear functions Summary Agenda
  3. Special Cases: 1. If either x or y is Positive

    Infinity, the function returns Infinity. 2. If either x or y is NaN, the function returns NaN. 3. If both x and y are 0, and the sign bit of x is Negative, meaning Signbit(x) is true, then it returns y. This scenario addresses the comparison between -0.0 and 0.0. Source Code Prior to the built-in min and max functions
  4. func clear func clear[T ~[]Type | ~map[Type]Type1](t T) If the

    argument type is a type parameter, the type parameter's type set must contain only map or slice types, and clear performs the operation implied by the type argument. For maps, clear deletes all entries, resulting in an empty map. For slices, clear sets all elements up to the length of the slice to the zero value of the respective element type. — The clear built-in function clears maps and slices. Clear Function 代表任何 Go 的類型
  5. type Type type Type int type Type1 type Type1 int

    Type is here for the purposes of documentation only. It is a stand-in for any Go type, but represents the same type for any given function invocation. Type1 is here for the purposes of documentation only. It is a stand-in for any Go type, but represents the same type for any given function invocation. Clear Function
  6. Reference • min/max being builtins vs being in package cmp

    • Unique Capability of the clear Function: Removing Irreflexive Keys with NaN Values from Maps
  7. Summary • Makes it easier for beginners to understand. •

    Reduces the development workload for Gophers.