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

Go Release 1.20

Go Release 1.20

Introduce changes in Go 1.20 to our audience.

Avatar for Ladislav Prskavec

Ladislav Prskavec

February 21, 2023
Tweet

More Decks by Ladislav Prskavec

Other Decks in Programming

Transcript

  1. G r le se 1.20 L v P c (P

    S ) - @a @h r .i - M P #1 , 2 .2. 2 1
  2. T me in 4 1 .1.2 - 1.2 R 4

    1 .1.2 - d e s 4 1.2.2 - 1.2 r d! 4 4-5.2.2 - F 2 - M E - S G 4 6.2.2 - u o'G - 1.2 o ! ... 4 1 .2.2 - 1.2 .1 4 1 .2.2 - T #2 - W 's e 1.2 4 2 .2.2 - u m ;-) L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 2
  3. R le se ot s 4 g .d /d /g

    .2 4 g .2 L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 3
  4. L ng ag c an es 4 g s: comparable

    s i c {} 4 n n SliceData, String, StringData unsafe p 4 d c i o a s a 4 e o w a e s n s e a c d L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 4
  5. G ne ic : C mp ra le on tr

    in func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V { var s V for _, v := range m { s += v } return s } 4 N s y c b interface{}, l r u p r ! L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 5
  6. S mp if s nt x li e -> a

    ra s 1.1 slice := []int{1,2,3, 4} array := (*[4]byte)(slice) s 1.2 slice := []int{1,2,3, 4} array := [4](slice) L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 6
  7. T ol mp ov me ts 4 c o s

    p l 4 g b , i a -pgo n -cover a 4 c d d d 4 g e i m L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 7
  8. G v t package main import ( "fmt" "time" )

    func main() { now := time.Now() now.Format("2006-02-01") fmt.Println(now) } $ go vet main.go # command-line-arguments ./main.go:10:14: 2006-02-01 should be 2006-01-02 L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 8
  9. C ve p o l s n ac ag l

    ve go test -coverprofile=... <pkg_target> 4 h ://g .d /t /c e/ L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 9
  10. B il w th ov r go build -cover -o

    myprogram.exe . go build -cover -o myprogramMorePkgs.exe -coverpkg=io,mydomain.com,rsc.io/quote .` L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 1
  11. R n o et at mkdir somedata GOCOVERDIR=somedata ./myprogram.exe GOCOVERDIR=somedata

    ./myprogram.exe --flag 4 y a u i m e t , c r p l L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 1
  12. W rk ng it c ve ag d ta l

    s go tool covdata <mode> -i=<dir1,dir2,...> ...flags... go tool covdata percent -i=somedata go tool covdata merge -i=windows_datadir,macos_datadir -o merged L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 1
  13. S an ar l br ry dd ti ns 4

    T e crypto/ecdh p p s e t s o E c C D e-H e e e e S c n C 5 . 4 T e f n e .J r e w g s e w a o d a h e p i n h Unwrap() []error m . 4 T e http.ResponseController p p s a e d p - r f n o h h http.ResponseWriter i c . 4 T httputil.ReverseProxy f i p i s e R o f n, s d h p s D r h . L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 1
  14. S an ar l br ry dd ti ns I

    4 T e context.WithCancelCause f n p s a c a c t a g e . a e a r e b c h e context.Cause f n. 4 T e os/exec.Cmd e C n W a s h b r h m e t a t C c d i p e . 4 I time p e a e e c t DateTime, DateOnly n TimeOnly L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 1
  15. E ro s Unwrap() error Unwrap() []error wrapsErr := fmt.Errorf("...

    %w ...", ..., err, ...) if errors.Is(err, fs.ErrExist) err1 := errors.New("err1") err2 := errors.New("err2") err := errors.Join(err1, err2) L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 1
  16. R sp ns Co tr ll r func RequestHandler(w ResponseWriter,

    r *Request) { rc := http.NewResponseController(w) rc.SetWriteDeadline(time.Time{}) // disable Server.WriteTimeout when sending a large response io.Copy(w, bigData) } 4 G I : n /h : a m a t s H L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 1
  17. h tp.R ve se ro y proxyHandler := &httputil.ReverseProxy{ Rewrite:

    func(r *httputil.ProxyRequest) { r.SetURL(outboundURL) // Forward request to outboundURL. r.SetXForwarded() // Set X-Forwarded-* headers. r.Out.Header.Set("X-Additional-Header", "header set by the proxy") }, } 4 T e R e s e h i g r f h o g r n d a e h m t k e w a s f i p . L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 1
  18. c nt xt.W th an el au e [^1] ctx,

    cancel := context.WithCancelCause(parent) cancel(myError) ctx.Err() // returns context.Canceled context.Cause(ctx) // returns myError L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 1
  19. o /e ec.C d ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) defer

    cancel() if err := exec.CommandContext(ctx, "sleep", "5").Run(); err != nil { // This will fail after 100 milliseconds. The 5 second sleep // will be interrupted. } 4 T e m e Cancel n WaitDelay s h b r o h m e t a t C c d t p e t I/O p s l e a c p . L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 1
  20. C tP e x / C tS x s :=

    "https://github.com" str, ok := strings.CutPrefix(s, "https://") if ok { fmt.Println(str) } else { fmt.Println(s) } L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 2
  21. T me on ta t pd te DateTime = "2006-01-02

    15:04:05" DateOnly = "2006-01-02" TimeOnly = "15:04:05" L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 2
  22. I pr ve p rf rm nc 4 C r

    n g c o i m v r m o d n i d o P p a 2%. 4 W s c t n c t t e b i m 1 %. i b b s c n t 1.1 . L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 2
  23. R fe en es C M. J n i s

    p r g a r 1.2 4 h ://b .c h .n /p /2 /g -1 -l e- c / 4 h ://b .c h .n /p /2 /g -1 -a - e -r e l / 4 h ://b .c h .n /p /2 /g -1 -m - f s/ L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 2
  24. w w.g me tu pr gu .c @g me tu

    pr gu @f ss od n.o g L v P c (P S ) - @a @h r .i - M P #1 , 2 .2. 2 2