Go 1.18 is officially released! Generics support, performance optimization...

Seven months after the release of Go 1.17, Go 1.18 is officially released! Go 1.18 is a release that includes a ton of new features, including the biggest ever change to the language itself (generics), toolchain implementation, runtime and library changes, and performance improvements.

As always, this release maintains the Go 1 compatibility promise : almost all Go programs continue to compile and run as before. Let's take a look at some of the major features of the new version:

Generics

Below is a list of the most notable changes to generics in Go 1.18, see the Generics proposal for a more comprehensive overview and the language specification for more details .

  • The syntax for function and type declarations now accepts type parameters .
  • Parameterized functions and types can be instantiated by listing the type parameters in square brackets .
  • New tokens ~have been added to Operators and Punctuation .
  • The syntax for interface types now allows embedding arbitrary types (not just the type names of interfaces) as well as union and ~T type elements. , such an interface can only be used as a type constraint.
  • The new  predeclared identifier any is an alias for the empty interface and can be used instead  interface{}.
  • The new  predeclared identifier comparable is an interface that represents a collection of all types that can be used ==or  compared, and it can only be used as (or embedded in) type constraints.!=

There are three experimental packages that use generics that may be useful, these are in the x/exp repository; but their APIs are not guaranteed by the Go 1 compatibility promise:

The current generic implementation has the following known limitations:

  • The Go compiler cannot handle type declarations in generic functions or methods, and this limitation is planned to be lifted in Go 1.19.
  • The Go compiler does not accept arguments with parameter types of the pre-declared functions real, imag and complex, this restriction is planned to be lifted in Go 1.19.
  • The Go compiler only supports calling method m on a value x of type parameter type P if m is explicitly declared by the constraint interface of P. Similarly, method values ​​xm and method expressions Pm are only supported if m is explicitly declared by P, even though m may be in P's method set, since all types in P implement m, planned in Go 1.19 Remove this restriction.
  • The Go compiler does not support accessing struct field xf, where x is the type parameter type, even though all types in the type parameter's type set have field f, this restriction is planned to be lifted in Go 1.19.
  • Embedding a type parameter or a pointer to a type parameter as an unnamed field in a struct type is not allowed, and similarly, embedding a type parameter in an interface type is not allowed.
  • A union element with multiple terms  may not contain an interface type with a non-empty method set.

Generics represent a huge change in the Go ecosystem, and while several core tools to support generics have been officially updated, there is still a lot of work to be done. It will take some time for the remaining tools, documentation, and libraries to catch up with these language changes. In addition, there is this paragraph in the official announcement:

  • There may be some code that uses generics that will work in version 1.18, but will break in later versions.
  • We do not plan or expect to make any such changes, however, for reasons we cannot foresee today, it may be necessary to break 1.18's programs in a future release .
  • We encourage the use of generics where it makes sense, but exercise caution when deploying generic code in production.

(Although generics have been made, it is likely to have bugs and it is not recommended to use them in production)

Fuzzing

  • Go 1.18 includes an implementation of fuzzing (fuzzing) as described  in the fuzzing proposal , see the  fuzzing tutorial for details to get started.
  • Note that fuzzing consumes a lot of memory and may affect the performance of the machine when it is running.
  • Also note that the fuzzing engine writes the value of extended test coverage to the fuzzing cache directory at runtime  $GOCACHE/fuzz. There is currently no limit to the number of files or total bytes that can be written to the obfuscated cache , so it can take up a lot of storage space (perhaps in the GB level).

translater

  • The compiler can now inline a containing range loop or a function marked as a loop.
  • The compiler's type checker is completely replaced to support generics, and some error messages may be worded differently than before (with more details, or in a more useful way).
  • Go 1.18 can compile about 15% slower than Go 1.17 due to compiler changes related to supporting generics , the execution time of the code is not affected, and the compiler speed is currently planned in Go 1.19.

Bug fixes

  • The Go 1.18 compiler can correctly report errors for variables that are set in function literals but never used (declared but not used), addressing an old issue  issue  #8560  .
  • The Go 1.18 compiler now reports overflow when a runic constant expression like '1' << 32 is passed as an argument to the predeclared functions print and println.

Ports

AMD64

Go 1.18 introduced a new GOAMD64environment variable that selects the minimum target version for the AMD64 architecture at compile time, the allowed values ​​are v1v2, v3or v4, and the default is v1.

RISC-V

The 64-bit RISC-V architecture on Linux (linux/riscv64 port) now supports c-archive and c-shared build modes.

Linux

Go 1.18 requires Linux kernel version 2.6.32 or higher.

Windows

The windows/arm and windows/arm64 sides are supporting non-cooperative preemption, which hopefully resolves some subtle bugs encountered when calling Win32 functions that would block for a long time.

iOS

Go 1.18 now requires iOS 12 or later on iOS (ios/arm64 port) and the iOS Simulator running on AMD64-based macOS (ios/amd64 port); support for previous versions has been discontinued.

FreeBSD

Go 1.18 is the last release to support FreeBSD 11.x, Go 1.19 requires FreeBSD 12.2+ or FreeBSD 13.0+.

performance boost

Since the register ABI calling convention in Go1.17 is extended to  RM64 / Apple M1 / ​​PowerPC 64  architectures, Go1.18 includes up to 20% CPU performance improvement for these architectures.

 

This Go 1.18 release also includes a number of other updates, the full list of updates can be found in the release bulletin .

Go 1.18 related links

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324510876&siteId=291194637