Go 1.22中可以 range 一个整数,比如下面的代码:123fori :=range10{fmt.Println(i)}这个大家都已经知道了,其实对应的提案中还有一个隐藏的功能,就是可以 range 一个函数,比如下面的代码(摘自官方代码库internal/trace/v2/event.go):12345678910111213141516171819// Frames is an iterator over the frames in a Stack.func(s Stack) Frames(yieldfunc(f StackFrame)bool)bool{ifs.id ==0{returntrue}stk := s.table.stacks.mustGet(s.id)for_, f :=rangestk.frames {sf := StackFrame{PC: f.pc,Func: s.table.strings.mustGet(f.funcID),File: s.table.strings.mustGet(f.fileID),Line: f.line,}if!yield(sf) {returnfalse}}returntrue}就少有介绍了。本文尝试介绍它,让读者先了解一下,它在Go 1.22 中是一个实验性的功能,还不确定未来在哪个版本中会被正式支持。官方wiki
...
继续阅读
(83)