search
HomeBackend DevelopmentGolangWhat do you know about Go1.20: PGO, compilation speed, error handling and other new features?

Recently Go1.20 was officially released in early February. It came very early. In the past, it was delayed until the end of the month. I read several articles and found that I still did a lot in the end. Functional trade-offs, forced to let go of some new features (for example: arena, etc.)!

I wonder if the Go team has anything to do in February, or is it planning to take a vacation? Or are you worried that layoffs will affect work handover?

Today we will quickly review the new features that are more relevant to us and see if it can be upgraded to 1.20.

Improving compilation speed

Before Go1.18 officially released generics, there were both joys and worries. Although this supports generics, the compilation speed in Go1.18 is slower than that in Go1.17, about 15-18% slower, which is a significant slowdown.

What do you know about Go1.20: PGO, compilation speed, error handling and other new features?

The generic feature has slowed down the build speed that Go is proud of. Are you afraid that you can make coffee if you build it later?

Originally it was said to be fixed in Go1.19, but it was later fixed. Finally, the current version has been fixed.

The following test report:

                │ 117.results │            118.results            │            119.results            │            tip.results            │
                │   sec/op    │   sec/op    vs base               │   sec/op    vs base               │   sec/op    vs base               │
GoBuildKubelet     52.58 ± 0%   56.54 ± 1%  +7.54% (p=0.000 n=10)   55.47 ± 1%  +5.50% (p=0.000 n=10)   51.41 ± 1%  -2.22% (p=0.000 n=10)
GoBuildIstioctl    47.78 ± 1%   51.44 ± 0%  +7.65% (p=0.000 n=10)   50.89 ± 5%  +6.50% (p=0.000 n=10)   46.05 ± 1%  -3.62% (p=0.000 n=10)
GoBuildFrontend    19.03 ± 1%   20.55 ± 1%  +7.99% (p=0.000 n=10)   20.04 ± 0%  +5.33% (p=0.000 n=10)   18.22 ± 1%  -4.27% (p=0.000 n=10)
geomean            36.29        39.10       +7.72%                  38.39       +5.77%                  35.07       -3.37%

In the latest Go1.20 benchmark test, the build speed of the current version and Go1.17 remains consistent.

In addition, the compiler and garbage collector are optimized, reducing memory overhead and improving overall CPU performance by 2%.

Go1.21 will end support for some versions of MacOS and Windows

The update announcement of Go1.20 also announced a major update end notification, involving macOS and windows operating system.

What do you know about Go1.20: PGO, compilation speed, error handling and other new features?

They are as follows:

  • Go1.20 is the last one to support running on macOS 10.13 High Sierra or 10.14 Mojave Version. Go 1.21 will require macOS 10.15 Catalina or later.

  • Go1.20 is the last version to support running on any version of Windows 7, 8, Server 2008, and Server 2012. Go 1.21 will require at least Windows 10 or Server 2016.

#Hey guys, it seems that I need to update my operating system version, otherwise Go will not welcome me to code in the next version.

What do you know about Go1.20: PGO, compilation speed, error handling and other new features?

有需要的同学在下个版本前尽早做好升级。

Go 发行版瘦身

新版本起,Go 的 $GOROOT/pkg 目录将不再存储标准库的预编译包存档,Go 发行版的将迎来一轮瘦身。

大小对比如下。

Go1.20:

What do you know about Go1.20: PGO, compilation speed, error handling and other new features?

Go1.19:

What do you know about Go1.20: PGO, compilation speed, error handling and other new features?

约比老版本缩减了 1/3,还是比较明显的。

PGO 引入

在 Go1.20 起,Go 引入了 Profile-guided optimization (PGO),翻译过来是使用配置文件引导的优化,当前为预览版本。

PGO 是一门编译器优化技术,能够在不改业务代码的情况下,给你的应用程序带来一定的性能提升。在 Go PGO 中将会依托 runtime/pprof 所生成的 profile 来完成。

What do you know about Go1.20: PGO, compilation speed, error handling and other new features?

结果上可以使得 Go tool(工具链)能根据运行时信息执行特定于应用程序和工作负载的优化。说明了就是想提高性能,不需要改业务代码。

具体可以详见:《PGO 是啥,咋就让 Go 更快更猛了?》

支持封装多个错误

在原有 Go1.13 的 errors API 上进行新增和修改,核心是支持一个错误可以封装多个错误的特性。

新特性例子:

func main() {
 err1 := errors.New("err1")
 err2 := errors.New("err2")
 err := errors.Join(err1, err2)
 fmt.Println(err)
 if errors.Is(err, err1) {
  fmt.Println("err is err1")
 }
 if errors.Is(err, err2) {
  fmt.Println("err is err2")
 }
}

输出结果:

err1
err2
err is err1
err is err2

具体可以详见:《Go1.20 继续小修小补 errors 库。。。》

新增 StringData, String, SliceData

Go 团队通过分析、搜索发现 reflect.SliceHeader 和 reflect.StringHeader:

type StringHeader struct {
   Data uintptr
   Len  int
}

在业内经常被滥用,使用不方便,很容易出现隐性问题。例如:Data 字段类型是 uintptr 不是 unsafe.Pointer。设什么都可以,灵活度过于高,非常容易搞出问题。

在 Go1.20 起,在 unsafe 标准库新增了 3 个函数来替代前面这两个类型的使用。希望能够进一步标准化,并提供额外的类型安全。

What do you know about Go1.20: PGO, compilation speed, error handling and other new features?

如下函数签名:

  • func String(ptr *byte, len IntegerType) string:根据数据指针和字符长度构造一个新的 string。
  • func StringData(str string) *byte:返回指向该 string 的字节数组的数据指针。
  • func SliceData(slice []ArbitraryType) *ArbitraryType:返回该 slice 的数据指针。

新版本的用法变成:

func StringToBytes(s string) []byte {
    return unsafe.Slice(unsafe.StringData(s), len(s))
}

func BytesToString(b []byte) string {
    return unsafe.String(&b[0], len(b))
}

以往常用的 reflect.SliceHeaderreflect.StringHeader 将会被标注为被废弃。

具体可以详见:《别乱用了,用新的。Go SliceHeader 和 StringHeader 将会被废弃!》

优化时间比较和格式记忆

2006-01-02 15:04:05

有很多 Go 同学反馈老要记 2006-01-02 15:04:05,发现这个日期时间点,使用的次数非常高频:

##511940time.RFC1123

使用频率的数据有理有据。

Go1.20 加了以下常量,便于直接引用:

DateTime   = "2006-01-02 15:04:05"
DateOnly   = "2006-01-02"
TimeOnly   = "15:04:05"

Time.Compare

再者就是新增了时间比较的方法。

在现在的标准库中,有 3 个方法来比较 time.Time 对象,分别是:Before()、Equal() 和 After(),作用上类似 。但缺少 = 的等价物。

Go1.20 将会支持 Time.Compare,以此来达到类似的效果。作用是将 Time 对象 t 和 u 两者进行比较。

func (t Time) Compare(u Time) int

该方法返回如下几种结果:

  • 如果 t 在 u 之前,则返回 -1。
  • 如果 t 在 u 之后,则返回 +1。
  • 如果它们相同,则返回 0。

具体可以详见:《Go1.20 中两个关于 Time 的更新,终于不用背 2006-01-02 15:04:05 了!》

禁用匿名接口循环导入

以前可以做匿名接口循环导入的骚操作。如下代码:

type I interface {
 m()
 interface {
     I
  }
}

这段代码,声明了接口类型 I,然后又包含了 m(),又包含接口 I。这会是一个 “永动机”,永远都不会停止。在开源的 GitHub 中,也真实存在着。

Go1.20 起,编译器将会默认拒绝匿名接口循环导入。如果没有用户反馈受到了重大的影响或问题,将会计划在 Go1.22 中正式的禁用和移除该项功能的支持。

具体可以详见:《Go1.20 将禁止匿名接口循环导入!》

没有 C工具链默认禁用 CGO

Go1.20 将会在没有 C 工具链的系统上默认禁用 CGO。这理论上是一个不兼容性设置,如果大家有需要,可以提前设置好 CGO_ENABLED 环境变量,以避免导致部分应用程序出问题。

支持切片到数组的转换

Go1.20 起支持将切片转换成数组。

如下代码:

func main() {
 v := []string{"煎", "鱼", "进", "脑", "子", "了"}
 s := [6]string(v)
 fmt.Println(s)
}

当然,前提是切片和数字的长度和类型都要对的上。否则会出现如下报错:

panic: runtime error: cannot convert slice with length 5 to array or pointer to array with length 6

goroutine 1 [running]:
main.main()
 /tmp/sandbox1162344488/prog.go:9 +0x1d

Program exited.

总结

在本次 Go1.20 的更新中,比较有意思的是 PGO 的预览版本,大家有机会可以体验下不改代码就提高应用性能的快感。而相关的更新有的是在偿还技术债务。例如:编译加速等。

Arena, which originally attracted a lot of attention, was previously featured in "Slap in the face, brothers, Go1.20 arena is here!" I shared it in . After specific implementation and analysis, the Go team found that there were serious problems with the existing API, and temporarily rolled back the iteration code, so they gave up. I plan to share this separately later.

It is particularly important to note that starting from Go1.21, some versions of macOS and windows will no longer be supported. Maybe some companies’ machines, or even your own, need to be upgraded in advance

Ranking Frequency Format
1 75616 time.RFC3339
2 23954 time.RFC3339Nano
3 13312 "2006-01-02 15:04:05"
4 12332 "2006-01-02"

The above is the detailed content of What do you know about Go1.20: PGO, compilation speed, error handling and other new features?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Golang菜鸟. If there is any infringement, please contact admin@php.cn delete
init Functions and Side Effects: Balancing Initialization with Maintainabilityinit Functions and Side Effects: Balancing Initialization with MaintainabilityApr 26, 2025 am 12:23 AM

Toensureinitfunctionsareeffectiveandmaintainable:1)Minimizesideeffectsbyreturningvaluesinsteadofmodifyingglobalstate,2)Ensureidempotencytohandlemultiplecallssafely,and3)Breakdowncomplexinitializationintosmaller,focusedfunctionstoenhancemodularityandm

Getting Started with Go: A Beginner's GuideGetting Started with Go: A Beginner's GuideApr 26, 2025 am 12:21 AM

Goisidealforbeginnersandsuitableforcloudandnetworkservicesduetoitssimplicity,efficiency,andconcurrencyfeatures.1)InstallGofromtheofficialwebsiteandverifywith'goversion'.2)Createandrunyourfirstprogramwith'gorunhello.go'.3)Exploreconcurrencyusinggorout

Go Concurrency Patterns: Best Practices for DevelopersGo Concurrency Patterns: Best Practices for DevelopersApr 26, 2025 am 12:20 AM

Developers should follow the following best practices: 1. Carefully manage goroutines to prevent resource leakage; 2. Use channels for synchronization, but avoid overuse; 3. Explicitly handle errors in concurrent programs; 4. Understand GOMAXPROCS to optimize performance. These practices are crucial for efficient and robust software development because they ensure effective management of resources, proper synchronization implementation, proper error handling, and performance optimization, thereby improving software efficiency and maintainability.

Go in Production: Real-World Use Cases and ExamplesGo in Production: Real-World Use Cases and ExamplesApr 26, 2025 am 12:18 AM

Goexcelsinproductionduetoitsperformanceandsimplicity,butrequirescarefulmanagementofscalability,errorhandling,andresources.1)DockerusesGoforefficientcontainermanagementthroughgoroutines.2)UberscalesmicroserviceswithGo,facingchallengesinservicemanageme

Custom Error Types in Go: Providing Detailed Error InformationCustom Error Types in Go: Providing Detailed Error InformationApr 26, 2025 am 12:09 AM

We need to customize the error type because the standard error interface provides limited information, and custom types can add more context and structured information. 1) Custom error types can contain error codes, locations, context data, etc., 2) Improve debugging efficiency and user experience, 3) But attention should be paid to its complexity and maintenance costs.

Building Scalable Systems with the Go Programming LanguageBuilding Scalable Systems with the Go Programming LanguageApr 25, 2025 am 12:19 AM

Goisidealforbuildingscalablesystemsduetoitssimplicity,efficiency,andbuilt-inconcurrencysupport.1)Go'scleansyntaxandminimalisticdesignenhanceproductivityandreduceerrors.2)Itsgoroutinesandchannelsenableefficientconcurrentprogramming,distributingworkloa

Best Practices for Using init Functions Effectively in GoBest Practices for Using init Functions Effectively in GoApr 25, 2025 am 12:18 AM

InitfunctionsinGorunautomaticallybeforemain()andareusefulforsettingupenvironmentsandinitializingvariables.Usethemforsimpletasks,avoidsideeffects,andbecautiouswithtestingandloggingtomaintaincodeclarityandtestability.

The Execution Order of init Functions in Go PackagesThe Execution Order of init Functions in Go PackagesApr 25, 2025 am 12:14 AM

Goinitializespackagesintheordertheyareimported,thenexecutesinitfunctionswithinapackageintheirdefinitionorder,andfilenamesdeterminetheorderacrossmultiplefiles.Thisprocesscanbeinfluencedbydependenciesbetweenpackages,whichmayleadtocomplexinitializations

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools