Go 1.20 is coming, take a look at the changes - the end

Go 1.20 is coming, let's see what changes - Part 4

foreword

The official Go team released the Go 1.20 rc1 (release candidate) version on 2022.12.08, and the official release version of Go 1.20 is expected to be released in February 2023.

Let's take a sneak peek and see what changes Go 1.20 brings us.

installation method:

$ go install golang.org/dl/go1.20rc1@latest
$ go1.20rc1 download

This is the fourth part of the detailed explanation of the update content of Go 1.20 version. You are welcome to pay attention to the official account and get the latest update of this series in time.

Go 1.20 release checklist

Compared with Go 1.19, the changes are moderate, mainly involving language (Language), portability (Ports), tool chain (Go Tools), runtime (Runtime), compiler (Compiler), assembler (Assembler), link Optimization of linker and core library.

The first article mainly involves the optimization of Go 1.20 in terms of language and portability. The original text link: Go 1.20 Version Upgrade Content Part 1 .

The second part mainly involves the optimization of Go commands and tool chains. The link to the original text: Go 1.20 version upgrade content part 2 .

Part 3 mainly involves the optimization of Go in terms of runtime, compiler, assembler, linker, etc. Link to the original text: Part 3 of Go 1.20 version upgrade content .

This article focuses on the optimization of Go 1.20 in the core library.

crypto/ecdh

Go 1.20 added crypto/ecdhthis package ecdhto implement the new encryption algorithm Elliptic Curve Diffie-Hellman.

Encapsulate multiple errors

Go 1.20 allows multiple errors to be encapsulated in an error variable.

package main

import (
	"errors"
	"fmt"
)

func main() {
    
    
	err1 := errors.New("err1")
	err2 := errors.New("err2")
	err := errors.Join(err1, err2)
	fmt.Printf("%T, %v\n", err, err)
	if errors.Is(err, err1) {
    
    
		fmt.Println("err is err1")
	}
	if errors.Is(err, err2) {
    
    
		fmt.Println("err is err2")
	}
	err3 := fmt.Errorf("error3: %w", err)
	fmt.Printf("%T, %v\n", err3, errors.Unwrap(err3))
	if errors.Is(err3, err1) {
    
    
		fmt.Println("err3 is err1")
	}
	if errors.Is(err3, err2) {
    
    
		fmt.Println("err3 is err2")
	}
}

The output of this program is:

*errors.joinError, err1
err2
err is err1
err is err2
*fmt.wrapError, err1
err2
err3 is err1
err3 is err2

For details, please refer to: https://pkg.go.dev/errors@master#pkg-overview

fmt.ErrorfWith %wparameters in it, it will return a variable of error type that implements the Unwrap method.

HTTP ResponseController

net/httpThis package adds ResponseControllera new type named .

func RequestHandler(w ResponseWriter, r *Request) {
    
    
  rc := http.NewResponseController(w)
  rc.SetWriteDeadline(0) // disable Server.WriteTimeout when sending a large response
  io.Copy(w, bigData)
}

A ResponseController is used by an HTTP handler to control the response.

A ResponseController may not be used after the Handler.ServeHTTP method has returned.

For details, please refer to: https://pkg.go.dev/net/http@master#ResponseController.

Rewrite hook function

httputil.ReverseProxyA new Rewritemethod , which is a hook function to replace the previous Directorhook function.

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")
  },
}

For details, please refer to: https://pkg.go.dev/net/http/httputil@master#ReverseProxy.Rewrite.

Modifications to the standard library

  • bytes

    CutPrefixThe and functions are added CutSuffix. These two functions are similar TrimPrefixto and TrimSuffix, but they also return a variable of bool type, indicating whether the string has been modified.

    Added Clonefunction , which will create a copy of byte slice.

  • encoding/binary

    ReadVarintReadUvarintIf the value of the read data is damaged, such as only part of the content is written, the function will return io.ErrUnexpectedEOFinstead of returning as before io.EOF.

  • errors

    The new Joinfunction can combine the values ​​of multiple error variables and encapsulate them into a new error variable.

  • fmt

    ErrorfSupports %wformatting strings, and can return an error type variable that implements the Unwrap method.

  • strings

    Added CutPrefixand CutSuffixfunction , these two functions are similar TrimPrefixto and TrimSuffix, but will also return a variable of bool type, indicating whether the string has been modified.

    A new Clonefunction , which will create a copy of string.

  • sync

    MapThree new methods have been added to the type: Swap, CompareAndSwapand CompareAndDelete, allowing atomic updates to existing maps.

  • testing

    A new method has been added B.Elapsedto return how long the current benchmark performance test took.

  • time

    Added 3 constants DateTime, DateOnlyand TimeOnly, which are convenient for developers to do format conversion, without writing "2006-01-02 15:04:05" in the code.

Summarize

Go version 1.20 will be released in February 2023.

Generally speaking, Go 1.20 version has no major modifications, and various optimizations have been made mainly in details.

recommended reading

open source address

Articles and sample code are open source on GitHub: Go Language Beginner, Intermediate, and Advanced Tutorials .

Official account: advanced coding. Follow the official account to get the latest Go interview questions and technology stack.

Personal website: Jincheng's Blog .

Zhihu: Wuji .

Welfare

I have compiled a gift pack of back-end development learning materials for you, including programming language entry to advanced knowledge (Go, C++, Python), back-end development technology stack, interview questions, etc.

Follow the official account "coding advanced", send a message to backend to receive a data package, this data will be updated from time to time, and add data that I think is valuable. You can also send a message " Join the group " to communicate and learn with your peers, and answer questions.

References

  • https://tip.golang.org/doc/go1.20# Go 1.20 is coming, take a look at the changes - Part 4

foreword

The official Go team released the Go 1.20 rc1 (release candidate) version on 2022.12.08, and the official release version of Go 1.20 is expected to be released in February 2023.

Let's take a sneak peek and see what changes Go 1.20 brings us.

installation method:

$ go install golang.org/dl/go1.20rc1@latest
$ go1.20rc1 download

This is the fourth part of the detailed explanation of the update content of Go 1.20 version. You are welcome to pay attention to the official account and get the latest update of this series in time.

Go 1.20 release checklist

Compared with Go 1.19, the changes are moderate, mainly involving language (Language), portability (Ports), tool chain (Go Tools), runtime (Runtime), compiler (Compiler), assembler (Assembler), link Optimization of linker and core library.

The first article mainly involves the optimization of Go 1.20 in terms of language and portability. The original text link: Go 1.20 Version Upgrade Content Part 1 .

The second part mainly involves the optimization of Go commands and tool chains. The link to the original text: Go 1.20 version upgrade content part 2 .

Part 3 mainly involves the optimization of Go in terms of runtime, compiler, assembler, linker, etc. Link to the original text: Part 3 of Go 1.20 version upgrade content .

This article focuses on the optimization of Go 1.20 in the core library.

crypto/ecdh

Go 1.20 added crypto/ecdhthis package ecdhto implement the new encryption algorithm Elliptic Curve Diffie-Hellman.

Encapsulate multiple errors

Go 1.20 allows multiple errors to be encapsulated in an error variable.

package main

import (
	"errors"
	"fmt"
)

func main() {
    
    
	err1 := errors.New("err1")
	err2 := errors.New("err2")
	err := errors.Join(err1, err2)
	fmt.Printf("%T, %v\n", err, err)
	if errors.Is(err, err1) {
    
    
		fmt.Println("err is err1")
	}
	if errors.Is(err, err2) {
    
    
		fmt.Println("err is err2")
	}
	err3 := fmt.Errorf("error3: %w", err)
	fmt.Printf("%T, %v\n", err3, errors.Unwrap(err3))
	if errors.Is(err3, err1) {
    
    
		fmt.Println("err3 is err1")
	}
	if errors.Is(err3, err2) {
    
    
		fmt.Println("err3 is err2")
	}
}

The output of this program is:

*errors.joinError, err1
err2
err is err1
err is err2
*fmt.wrapError, err1
err2
err3 is err1
err3 is err2

For details, please refer to: https://pkg.go.dev/errors@master#pkg-overview

fmt.ErrorfWith %wparameters in it, it will return a variable of error type that implements the Unwrap method.

HTTP ResponseController

net/httpThis package adds ResponseControllera new type named .

func RequestHandler(w ResponseWriter, r *Request) {
    
    
  rc := http.NewResponseController(w)
  rc.SetWriteDeadline(0) // disable Server.WriteTimeout when sending a large response
  io.Copy(w, bigData)
}

A ResponseController is used by an HTTP handler to control the response.

A ResponseController may not be used after the Handler.ServeHTTP method has returned.

For details, please refer to: https://pkg.go.dev/net/http@master#ResponseController.

Rewrite hook function

httputil.ReverseProxyA new Rewritemethod , which is a hook function to replace the previous Directorhook function.

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")
  },
}

For details, please refer to: https://pkg.go.dev/net/http/httputil@master#ReverseProxy.Rewrite.

Modifications to the standard library

  • bytes

    CutPrefixThe and functions are added CutSuffix. These two functions are similar TrimPrefixto and TrimSuffix, but they also return a variable of bool type, indicating whether the string has been modified.

    Added Clonefunction , which will create a copy of byte slice.

  • encoding/binary

    ReadVarintReadUvarintIf the value of the read data is damaged, such as only part of the content is written, the function will return io.ErrUnexpectedEOFinstead of returning as before io.EOF.

  • errors

    The new Joinfunction can combine the values ​​of multiple error variables and encapsulate them into a new error variable.

  • fmt

    ErrorfSupports %wformatting strings, and can return an error type variable that implements the Unwrap method.

  • strings

    Added CutPrefixand CutSuffixfunction , these two functions are similar TrimPrefixto and TrimSuffix, but will also return a variable of bool type, indicating whether the string has been modified.

    A new Clonefunction , which will create a copy of string.

  • sync

    MapThree new methods have been added to the type: Swap, CompareAndSwapand CompareAndDelete, allowing atomic updates to existing maps.

  • testing

    A new method has been added B.Elapsedto return how long the current benchmark performance test took.

  • time

    Added 3 constants DateTime, DateOnlyand TimeOnly, which are convenient for developers to do format conversion, without writing "2006-01-02 15:04:05" in the code.

Summarize

Go version 1.20 will be released in February 2023.

Generally speaking, Go 1.20 version has no major modifications, and various optimizations have been made mainly in details.

recommended reading

open source address

Articles and sample code are open source on GitHub: Go Language Beginner, Intermediate, and Advanced Tutorials .

Official account: advanced coding. Follow the official account to get the latest Go interview questions and technology stack.

Personal website: Jincheng's Blog .

Zhihu: Wuji .

Welfare

I have compiled a gift pack of back-end development learning materials for you, including programming language entry to advanced knowledge (Go, C++, Python), back-end development technology stack, interview questions, etc.

Follow the official account "coding advanced", send a message to backend to receive a data package, this data will be updated from time to time, and add data that I think is valuable. You can also send a message " Join the group " to communicate and learn with your peers, and answer questions.

References

  • https://tip.golang.org/doc/go1.20

Guess you like

Origin blog.csdn.net/perfumekristy/article/details/128506065