DEV Community

Cover image for Golang 1.21 is here (Part 3)
Pedro Bertao
Pedro Bertao

Posted on

Golang 1.21 is here (Part 3)

This final part we will be looking at some features of the new slices packages which brings a lot of awesome features.

First we are going to use slices.Compare which is a very useful function when comparing two slices.

        intSliceA := []int{1, 2, 3}
        isEqual := slices.Compare(intSliceA, []int{1, 2, 3})
    if isEqual == 0 {
        fmt.Println("Is Equal")
    } else {
        fmt.Println("Not Equal")
    }
    // Is Equal

    isEqual = slices.Compare(intSliceA, []int{4, 5, 6})
    if isEqual == 0 {
        fmt.Println("Is Equal")
    } else {
        fmt.Println("Not equal")
    }
    // Not Equal
Enter fullscreen mode Exit fullscreen mode

Another great use of slices packages is slices.contains

        intSliceA := []int{1, 2, 3}
    isEqual := slices.Contains(intSliceA, 1)
    if isEqual {
        fmt.Println("Contains")
    } else {
        fmt.Println("Does not Contain")
    }
Enter fullscreen mode Exit fullscreen mode

Finally another interesting function is slices.Max where it locates the max value inside the slice as shown below

    intSliceA := []int{3, 123, 23, 57, 98, 4, 3, 2}

    max := slices.Max(intSliceA)
    fmt.Println("Max:", max)
    // Max:  123
Enter fullscreen mode Exit fullscreen mode

If you wanna to check more of the 1.21 slices package, you can click here

To finish this post I will quote the overview of changelog

The latest Go release, version 1.21, arrives six months after Go 1.20. Most of its changes are in the implementation of the toolchain, runtime, and libraries. As always, the release maintains the Go 1 promise of compatibility; in fact, Go 1.21 improves upon that promise. We expect almost all Go programs to continue to compile and run as before.
Go 1.21 introduces a small change to the numbering of releases. In the past, we used Go 1.N to refer to both the overall Go language version and release family as well as the first release in that family. Starting in Go 1.21, the first release is now Go 1.N.0. Today we are releasing both the Go 1.21 language and its initial implementation, the Go 1.21.0 release. These notes refer to “Go 1.21”; tools like go version will report “go1.21.0” (until you upgrade to Go 1.21.1). See “Go versions” in the “Go Toolchains” documentation for details about the new version numbering.

Go 1.21 brings a lot of features that will makes your code more cleaner and readable. It is must read if you want to use this version for your next projects.

Top comments (0)