语言行为变动

Go 1.20曾经于往年2月份公布,Go 1.21也不远了,咱们来先睹为快,看看Go 1.21版本里几个乏味的变动。

文末附送2道面试题。

panic(nil)

func main() {  defer func() {    print(recover() == nil)  }()  panic(nil)}

大家先想一想这段代码会输入什么?是true还是false。

在Go 1.20版本及以前会输入true。

然而在Go 1.21版本开始会输入false。这是因为Go 1.21定义了一个新的类型*runtime.PanicNilError

panic(nil)后,recover()会返回一个类型为*runtime.PanicNilError,值为panic called with nil argument的变量,具体能够参考如下代码:

func main() {    defer func() {        r := recover()        fmt.Printf("%T\n", r) // *runtime.PanicNilError        fmt.Println(r) // panic called with nil argument    }()    panic(nil)}

clear函数

Go 1.21会新增一个clear函数,用于清理map和slice里的元素。示例代码如下:

package mainimport "fmt"var x = 0.0var nan = x / xfunc main() {    s := []int{1, 2, 3}    clear(s)    fmt.Println(s) // [0 0 0]    m := map[float64]int{0.1: 9}    m[nan] = 5    clear(m)    fmt.Println(len(m)) // 0}

官网源码阐明如下:

// The clear built-in function clears maps and slices.

// For maps, clear deletes all entries, resulting in an empty map.

// For slices, clear sets all elements up to the length of the slice

// to the zero value of the respective element type. If the argument

// type is a type parameter, the type parameter's type set must

// contain only map or slice types, and clear performs the operation

// implied by the type argument.

func clear[T ~[]Type | ~map[Type]Type1](t T "T ~[]Type | ~map[Type]Type1")

对于map,调用clear函数,会间接把map里的元素清空,成为一个empty map。

对于slice,调用clear函数,会放弃原slice的长度不变,把外面元素的值批改为slice元素类型的零值。

面试题

defer语义是Go开发人员常常应用到的,也是最容易了解谬误的中央。

大家看看上面2道对于defer的程序会输入什么后果。

package mainimport "fmt"func f() {    defer func() {        defer func() { recover() }()        defer recover()        panic(2)    }()    panic(1)}func main() {    defer func() { fmt.Print(recover()) }()    f()}
  • A: 2
  • B: 1
  • C: nil
  • D: 抛panic异样
package mainimport "fmt"func main() {    for i := 0; i < 3; i++ {        defer func() { print(i) }()    }    for i := range [3]int{} {        defer func() { print(i) }()    }}
  • A: 222333
  • B: 210333
  • C: 333333
  • D: 210210

想晓得答案的发送音讯121到公众号。

举荐浏览

  • Go 1.20来了,看看都有哪些变动
  • Go面试题系列,看看你会几题
  • Go常见谬误和最佳实际系列

开源地址

文章和示例代码开源在GitHub: Go语言高级、中级和高级教程。

公众号:coding进阶。关注公众号能够获取最新Go面试题和技术栈。

集体网站:Jincheng's Blog。

知乎:无忌。

福利

我为大家整顿了一份后端开发学习材料礼包,蕴含编程语言入门到进阶常识(Go、C++、Python)、后端开发技术栈、面试题等。

关注公众号「coding进阶」,发送音讯 backend 支付材料礼包,这份材料会不定期更新,退出我感觉有价值的材料。还能够发送音讯「进群」,和同行一起交流学习,答疑解惑。

References

  • https://twitter.com/go100and1
  • https://twitter.com/go100and1...