How to implement code navigation in Go language?
In Go language, code jumps can be achieved using the goto statement. It allows the code to jump to a specified label and continue executing from that point.
Here is an example:
func main() {
i := 0
loop: // 定义一个标签
if i < 5 {
fmt.Println(i)
i++
goto loop // 跳转到标签处继续执行
}
}
The code above will output numbers ranging from 0 to 4.
It’s not recommended to use the goto statement in most cases because it can make the code difficult to understand and maintain. In most situations, it’s better to use looping statements (like for loops) or conditional statements (like if statements) to control the flow of code.