What's new in Go 1.21 and 1.22

19 Fev 2024

Mauri de Souza Meneguzzo

About me

2

Let's Go!

3

Go builds are now reproducible

4

Supply Chain Attacks

https://xkcd.com/2347/

5

VulnCheck v1.0.0

govulncheck cmd

go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...

Vulnerability #1: GO-2023-2102
 HTTP/2 rapid reset can cause excessive work in net/http
    More info: https://pkg.go.dev/vuln/GO-2023-2102
    Module: golang.org/x/net
        Found in: golang.org/x/[email protected]
        Fixed in: golang.org/x/[email protected]
        Example traces found:
        #1: main.go:34:16: server.main calls gin.Engine.Run, which eventually calls
        http2.Server.ServeConn
6

Go Toolchains

$ go version
go version devel go1.23-af5943f90c Sat Feb 17 23:25:55 2024 +0000 darwin/arm64
$ go env -w GOTOOLCHAIN=go1.22.0
$ go version
go version go1.22.0 darwin/arm64
7

GOOS=wasip1 port

8

Profile Guided Optimization

9

How to capture a profile?

# tests or benchmarks
go test -cpuprofile=cpu.prof -bench=. .

# net/http/pprof
go tool pprof http://localhost:6060/debug/pprof/profile

# runtime/pprof
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
10

Iterator Proposal

11

Range over integer

package main

func main() {
    for i := range 5 {
        println(i)
    }
}
func BenchA(b *testing.B) {
    for i := 0; i < b.N; i++ {
        // ...
    }
}

func BenchB(b *testing.B) {
    for range b.N {
        // ...
    }
}
12

Range over func, aka iterators

iter.Seq/iter.Seq2

package iter // import "iter"

// Seq is an iterator over sequences of individual values.
// When called as seq(yield), seq calls yield(v) for each value v 
// in the sequence, stopping early if yield returns false.
type Seq[V any] func(yield func(V) bool)

// Seq2 is an iterator over sequences of pairs of values, most commonly
// key-value pairs. When called as seq(yield), seq calls yield(k, v) for each
// pair (k, v) in the sequence, stopping early if yield returns false.
type Seq2[K, V any] func(yield func(K, V) bool)
13

package main

import "iter"

func main() {
    var mySeq iter.Seq[int]
    mySeq = func(yield func(n int) bool) {
        for i := 0; i < 5; i++ {
            if !yield(i) {
                return
            }
        }
    }

    for v := range mySeq {
        println(v)
    }
}
14

Dead Code Elimination

Go Blog about deadcode

$ go install golang.org/x/tools/cmd/deadcode@latest
$ deadcode ./...
15

log/slog

16

# implicit, simpler syntax
slog.Info("User logged in", "username", user.Name, "userid", user.ID)

# explicit, less memory allocs
slog.LogAttrs(context.Background(), slog.LevelInfo, "User logged in",
    slog.String("username", user.Name), slog.Int("userid", user.ID))

# zap for comparison
zapLogger.Info("User logged in",
    zap.String("username", user.Name),
    zap.Int("userid", user.ID))
17

math/rand/v2

First V2 package in the stdlib

Read removed. Many people misused math/rand.Read as a secure random source instead of crypto/rand.Read

Outdated, slow algorithms in math/rand

//go:linkname Uint32 runtime.fastrand
func Uint32() uint32

//go:linkname Uint32n runtime.fastrandn
func Uint32n(n uint32) uint32
18

net/http Routing enhancements

http.Handle("GET /posts/{id}", handlePost2)
// ...
idString := req.PathValue("id") 
19

Go 2 when?

20

21

Go 1.23 so far

22

Useful links

The Go home page.

Go blog

My talks are written with golang.org/x/tools/present

Find this talk at talks.mauri870.com

23

Thank you

19 Fev 2024

Mauri de Souza Meneguzzo

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)