$30 off During Our Annual Pro Sale. View Details »

Let's look at Go 1.18

Let's look at Go 1.18

Slides of the Milan Golang Meetup

omissis

June 14, 2022
Tweet

More Decks by omissis

Other Decks in Programming

Transcript

  1. Let's look at Go 1.18!
    Claudio Beatrice - June 6th, 2022
    Having fun fun fun 🕺 with


    Generics, Fuzzing and Workspace Mode

    View Slide

  2. Introducing Go 1.18
    This is the best iPhone we’ve ever made
    • It was released on Mar 15th, 2022 , seven months after Go 1.17 📆


    • It’s one of the major changes in the language ever done 🚛


    • It packs a TON of new features, plus library and tooling improvements


    • Generics 㿂


    • Fuzzing 🎛


    • Workspace mode 🗃


    • Sane packages for sorting stuff! 🥇


    • It keeps the Compatibility Promise, as usual 😎

    View Slide

  3. Theory and Practice
    In theory they are the same, in practice they aren't

    View Slide

  4. Generics
    One does not simply write speci
    f
    ic code
    • Write functions or types that work with any of a set of types provided by
    the calling code


    • Generic code is loosely coupled to the speci
    f
    ic types being used


    • Generics add three new, important features to the language:


    • Type parameters


    • Type inference


    • Interface types as sets of types


    • Create zero values out of generic types

    View Slide

  5. Generics
    Type Parameters
    func Min[T constraints.Ordered](x, y T) T {
    func Min(x, y int) int {
    As opposed to the an equivalent (for ints) monomorphic version

    View Slide

  6. Generics
    Type Inference
    x := Min[int](2, 3)
    x := Min(2, 3)
    Could be simpli
    f
    ied to

    View Slide

  7. Generics
    Type Sets
    package constraints
    type Ordered interface {
    Integer|Float|~string
    }
    Interfaces may now be used as type constraints,


    and they support union of different types.

    View Slide

  8. Generics
    Bracket beats chevron
    package main
    import "fmt"
    import "golang.org/x/exp/constraints"
    func Min[T constraints.Ordered](x, y T) T {
    if x < y {
    return x
    }
    return y
    }
    func main() {
    fmt.Println(Min[int](2, 3))
    fmt.Println(Min[float32](2.5, 2.6))
    }

    View Slide

  9. Generics
    Chevron cuts paper
    package main
    import "fmt"
    func Min(x, y int) int {
    if x < y {
    return x
    }
    return y
    }
    func main() {
    fmt.Println(Min(2, 3))
    fmt.Println(Min(2.5, 2.6)) // Error!
    }

    View Slide

  10. Generics
    It's showtime!
    Let's have a look at some examples:


    - interfaces: https://go.dev/play/p/PrqtLSmbVMi


    - generics: https://go.dev/play/p/RCnt4xM24mS


    - gondola's lang module

    View Slide

  11. Fuzzing
    Get cover!
    Fuzzing is a type of automated testing which continuously manipulates
    inputs to a program to
    f
    ind bugs.


    Good for
    f
    inding edge cases: it’s an extremely valuable feat whenever
    security is concerned, and still very valuable to hunt down hard-to-
    f
    ind
    bugs.

    View Slide

  12. Fuzzing
    Fuzz Anatomy 101

    View Slide

  13. Fuzzing
    Time to check gondola again
    One more thing

    View Slide

  14. Workspace mode
    It ain’t much, but it’s honest go work
    • A new, optional go.work
    f
    ile has been introduced: if present, it will set Go
    in Workspace Mode.


    • When in Workspace Mode, go will use the go.work
    f
    ile to determine all
    the modules to use as roots for module resolution, as opposed to having a
    single one as imposed by go.mod


    • Such Mode allows for work on multiple, independent modules at the same
    time more conveniently than it was in the past

    View Slide

  15. Workspace mode
    It ain’t much, but it’s honest go work
    go 1.18
    use ../foo/bar
    use ./baz
    replace example.com/foo v1.2.3 => example.com/bar v1.4.5

    View Slide

  16. Workspace mode
    it's the last time, I swear
    Open gondola once more

    View Slide

  17. New experimental packages
    Sort like it’s 2022
    • https://pkg.go.dev/golang.org/x/exp/constraints


    • https://pkg.go.dev/golang.org/x/exp/slices


    • https://pkg.go.dev/golang.org/x/exp/maps

    View Slide

  18. New networking package
    It’s a net improvement
    • New net/netip package, featuring Addr and AddrPort types


    • Compared to net.IP, netip.Addr is more memory ef
    f
    icient, immutable,
    comparable and can be used as a map key


    • Besides an IP and a port, AddrPort can also represent a CIDR pre
    f
    ix


    • Tons of new functions to work on the newly de
    f
    ined types


    • Loads of helpers for bridging with the old net.IP package

    View Slide

  19. And moar!
    Faster and furiouser, sometimes
    • More ef
    f
    icient way to pass function argument on arm64 and some amd64,
    getting up to 10% performance improvements


    • Due to generics, compile speed can get about 15% slower than go 1.17


    • Nice strings functions improvements, hello Cut()!


    • Ditto for bytes


    • Introduces the new any identi
    f
    ier as an alias for interface{}


    • Loads of other little minor changes

    View Slide

  20. Bibliography
    Slides are important, but study is importanter
    • Go 1.18 Release Notes


    • Article: An Introduction to Generics


    • Article: Are Go Generics ready for Prime Time?


    • Documentation: Fuzzing


    • Reference: Go Workspaces


    • Learning Go's free chapters on Generics

    View Slide

  21. Practice makes perfect
    Pick your poison
    https://go.dev/doc/tutorial/generics 㿂


    https://go.dev/doc/tutorial/fuzz 🎛


    https://go.dev/doc/tutorial/workspaces 🗃

    View Slide

  22. What about go 1.19?
    You thought it was over, huh
    • re
    f
    inements and improvements to generics


    • new unix build constraint


    • faster regex functions (~ -30% exec time)


    • faster large switch statements(int and string only)


    • more ef
    f
    icient race detector (1.5-2.0x faster, ~50%
    memory usage, unlimited goroutines)


    • lots of minor library changes, as usual

    View Slide

  23. Thanks
    And see you at the next meetup!

    View Slide

  24. Tell your friends!
    And your enemies, too

    View Slide