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/ecdh
this package ecdh
to 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.Errorf
With %w
parameters in it, it will return a variable of error type that implements the Unwrap method.
HTTP ResponseController
net/http
This package adds ResponseController
a 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.ReverseProxy
A new Rewrite
method , which is a hook function to replace the previous Director
hook 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
-
CutPrefix
The and functions are addedCutSuffix
. These two functions are similarTrimPrefix
to andTrimSuffix
, but they also return a variable of bool type, indicating whether the string has been modified.Added
Clone
function , which will create a copy of byte slice. -
ReadVarint
ReadUvarint
If the value of the read data is damaged, such as only part of the content is written, the function will returnio.ErrUnexpectedEOF
instead of returning as beforeio.EOF
. -
The new
Join
function can combine the values of multiple error variables and encapsulate them into a new error variable. -
Errorf
Supports%w
formatting strings, and can return an error type variable that implements the Unwrap method. -
Added
CutPrefix
andCutSuffix
function , these two functions are similarTrimPrefix
to andTrimSuffix
, but will also return a variable of bool type, indicating whether the string has been modified.A new
Clone
function , which will create a copy of string. -
Map
Three new methods have been added to the type:Swap
,CompareAndSwap
andCompareAndDelete
, allowing atomic updates to existing maps. -
A new method has been added
B.Elapsed
to return how long the current benchmark performance test took. -
Added 3 constants
DateTime
,DateOnly
andTimeOnly
, 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/ecdh
this package ecdh
to 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.Errorf
With %w
parameters in it, it will return a variable of error type that implements the Unwrap method.
HTTP ResponseController
net/http
This package adds ResponseController
a 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.ReverseProxy
A new Rewrite
method , which is a hook function to replace the previous Director
hook 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
-
CutPrefix
The and functions are addedCutSuffix
. These two functions are similarTrimPrefix
to andTrimSuffix
, but they also return a variable of bool type, indicating whether the string has been modified.Added
Clone
function , which will create a copy of byte slice. -
ReadVarint
ReadUvarint
If the value of the read data is damaged, such as only part of the content is written, the function will returnio.ErrUnexpectedEOF
instead of returning as beforeio.EOF
. -
The new
Join
function can combine the values of multiple error variables and encapsulate them into a new error variable. -
Errorf
Supports%w
formatting strings, and can return an error type variable that implements the Unwrap method. -
Added
CutPrefix
andCutSuffix
function , these two functions are similarTrimPrefix
to andTrimSuffix
, but will also return a variable of bool type, indicating whether the string has been modified.A new
Clone
function , which will create a copy of string. -
Map
Three new methods have been added to the type:Swap
,CompareAndSwap
andCompareAndDelete
, allowing atomic updates to existing maps. -
A new method has been added
B.Elapsed
to return how long the current benchmark performance test took. -
Added 3 constants
DateTime
,DateOnly
andTimeOnly
, 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