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.
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.
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.
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.
Go1.20 将会在没有 C 工具链的系统上默认禁用 CGO。这理论上是一个不兼容性设置,如果大家有需要,可以提前设置好 CGO_ENABLED 环境变量,以避免导致部分应用程序出问题。
支持切片到数组的转换
Go1.20 起支持将切片转换成数组。
如下代码:
func main() {
v := []string{"煎", "鱼", "进", "脑", "子", "了"}
s := [6]string(v)
fmt.Println(s)
}
Copy after login
当然,前提是切片和数字的长度和类型都要对的上。否则会出现如下报错:
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.
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
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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
In Go, WebSocket messages can be sent using the gorilla/websocket package. Specific steps: Establish a WebSocket connection. Send a text message: Call WriteMessage(websocket.TextMessage,[]byte("Message")). Send a binary message: call WriteMessage(websocket.BinaryMessage,[]byte{1,2,3}).
In Go, the function life cycle includes definition, loading, linking, initialization, calling and returning; variable scope is divided into function level and block level. Variables within a function are visible internally, while variables within a block are only visible within the block.
In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.
Go and the Go language are different entities with different characteristics. Go (also known as Golang) is known for its concurrency, fast compilation speed, memory management, and cross-platform advantages. Disadvantages of the Go language include a less rich ecosystem than other languages, a stricter syntax, and a lack of dynamic typing.
Memory leaks can cause Go program memory to continuously increase by: closing resources that are no longer in use, such as files, network connections, and database connections. Use weak references to prevent memory leaks and target objects for garbage collection when they are no longer strongly referenced. Using go coroutine, the coroutine stack memory will be automatically released when exiting to avoid memory leaks.
View Go function documentation using the IDE: Hover the cursor over the function name. Press the hotkey (GoLand: Ctrl+Q; VSCode: After installing GoExtensionPack, F1 and select "Go:ShowDocumentation").
Unit testing concurrent functions is critical as this helps ensure their correct behavior in a concurrent environment. Fundamental principles such as mutual exclusion, synchronization, and isolation must be considered when testing concurrent functions. Concurrent functions can be unit tested by simulating, testing race conditions, and verifying results.
When passing a map to a function in Go, a copy will be created by default, and modifications to the copy will not affect the original map. If you need to modify the original map, you can pass it through a pointer. Empty maps need to be handled with care, because they are technically nil pointers, and passing an empty map to a function that expects a non-empty map will cause an error.