Learn Go Generics - everything you need to know

Поділитися
Вставка
  • Опубліковано 29 січ 2025

КОМЕНТАРІ • 23

  • @joshuaward4725
    @joshuaward4725 2 роки тому +12

    This was the best explanation of go generics I've found, subscribed!

    • @leetwareltd
      @leetwareltd  2 роки тому

      Thanks mate, appreciate the kind words

  • @tam_sal80
    @tam_sal80 Місяць тому

    that was really helpful and so well explained 🎉

  •  2 місяці тому +1

    In your video at 8:52 Where have you defined type A,B,C?

  • @L0wPressure
    @L0wPressure Рік тому +1

    I was confused at first to see that TSM is doing golang videos nowadays...
    Quite a nice video with clear examples. Thanks :)

  • @fabrizziorosales9879
    @fabrizziorosales9879 Рік тому

    The best video that explains generics in go, Thanks :)

    • @leetwareltd
      @leetwareltd  Рік тому

      Thanks mate, means a lot to hear you say that 😊

  • @happy..1907
    @happy..1907 5 місяців тому

    Very well explained, thanks for creating this video. 👍

  • @rw_panic0_0
    @rw_panic0_0 Рік тому

    thanks, I really like the Currency example, shows what Go generics are capable of!

  • @_w62_
    @_w62_ 2 роки тому +1

    This is so far the best video on explaining GENERICS. 👍x10000

  • @a_maxed_out_handle_of_30_chars

    thank you :)

  • @berkansivrikaya9055
    @berkansivrikaya9055 Рік тому +2

    I don't get the last example. What is difference between `func PrintBalance[T Currency](b T )` with the function `func PrintBalance(b Currency)`?

    • @leetwareltd
      @leetwareltd  Рік тому +1

      Great question - my understanding is that you can't use type constraints (in this case Currency) as a type in a normal function - only in a generic function.
      The reason has to do with how the compiler works, non-generic functions always compile once to handle that function. But a generic function will compile multiple times for each type that it handles. The constraint `Currency` can't be used as a type to compile into a function which works for all implementations of `Currency` - so we need to use a generic function.
      At least this is how I remember it working in go 1.18 - it's been a while since I looked this deeply at it 😅

    • @manavshah5395
      @manavshah5395 9 місяців тому

      Great question. I still don't understand this bit.

    • @ioanvapi
      @ioanvapi 4 місяці тому

      @@leetwareltd You can use type constraint interfaces in non generic functions. When you define an interface you don't declair in any way that it is designed for generics or not. The differences are in other keys. For example, a non generic function with interface parameter is compiled to machine code once while a generic function compiles to more versions, one for each unique param types combinations. The compiler looks for each generic function invokation and creates at compile time a version for each different types params. So for example, if you have a generic function that you invoke it for int64 only then the compiler will compile just one version for this function using the int64 type. If it happens you invoke that generic function for both int64 and float64 then the compiler will compile one version of the function for int64 and one version for float64 and will invoke these accordingly. All these at compile time, therefore at runtime will run the appropriate machine code.
      The generic version provides more type safety and potentially better performance, but at the cost of increased compilation complexity and potential code bloat if used with many different types. The non-generic version offers more flexibility but with a small runtime cost for interface method dispatch.
      Thanks for your tutorials.

  • @computeering
    @computeering 9 місяців тому

    Broda u r just awesome

  • @ashutoshsinha5345
    @ashutoshsinha5345 2 роки тому

    Its the best so far for Generics

  • @dean-channel
    @dean-channel 2 роки тому

    Thanks, good tutorial!

  • @douglaspetrinbertini
    @douglaspetrinbertini 2 роки тому

    Thanks, very good content here!

  • @abhinandanbhattacharya1145
    @abhinandanbhattacharya1145 2 роки тому

    Great tutorial, thank you!

  • @mikolajsemeniuk8574
    @mikolajsemeniuk8574 Рік тому

    Nice video!

  • @stack.1
    @stack.1 9 місяців тому

    Voice over is pretty cloudy, but nice video

  • @siya.abc123
    @siya.abc123 10 місяців тому

    Golang is sexy!