Golang 死循环的多种写法

package main

import (
    "fmt"
)

func main() {
    endlessLoop6()
}

func endlessLoop1() {
    i := 10
    for {
        fmt.Println("i =", i)
    }
}

func endlessLoop2() {
    for i, j := 1, 10; i < j; i, j = i+1, j+1 {
        fmt.Println("i =", i, " j=", j) // j - i = 9
    }
}

func endlessLoop3() {
    i := 20
    select {
    default:
        fmt.Println("i =", i)
        endlessLoop3() // 递归
    }
}

func endlessLoop4() {
Here1:
    fmt.Println("Here1")
    goto Here2
Here2:
    fmt.Println("Here2")
    goto Here1
}

func endlessLoopEach5() {
    fmt.Println("Each5")
    defer endlessLoopOther5()
}

func endlessLoopOther5() {
    fmt.Println("Other5")
    defer endlessLoopEach5()
}

func endlessLoop6() {
    //定义一个匿名函数,函数的参数f是用一另外一个函数 endlessLoop6()的名称作为参数且另一个函数是无参数函数
    var loop = func(f func()) {
        fmt.Println("endlessLoop6")
        f()
        fmt.Println("1.本行代码永远都不会被执行") //本行上设置断点,调试运行本程序,启动和退出本程序时,均不会进入该断点
    }
    loop(endlessLoop6) //使用匿名函数去递归自己,此时执行f函数
    fmt.Println("2.本行代码永远都不会被执行") //本行上设置断点,调试运行本程序,启动和退出本程序时,均不会进入该断点
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Go语言实现循环单链表的示例代码: ```go package main import ( "fmt" ) type Node struct { data interface{} next *Node } type CircularLinkedList struct { head *Node tail *Node size int } func (list *CircularLinkedList) getSize() int { return list.size } func (list *CircularLinkedList) isEmpty() bool { return list.head == nil } func (list *CircularLinkedList) addFirst(data interface{}) { newNode := &Node{data, nil} if list.isEmpty() { list.head = newNode list.tail = newNode newNode.next = newNode } else { newNode.next = list.head list.head = newNode list.tail.next = newNode } list.size++ } func (list *CircularLinkedList) addLast(data interface{}) { newNode := &Node{data, nil} if list.isEmpty() { list.head = newNode list.tail = newNode newNode.next = newNode } else { newNode.next = list.head list.tail.next = newNode list.tail = newNode } list.size++ } func (list *CircularLinkedList) removeFirst() interface{} { if list.isEmpty() { return nil } data := list.head.data list.head = list.head.next list.tail.next = list.head list.size-- if list.isEmpty() { list.tail = nil } return data } func (list *CircularLinkedList) display() { if list.isEmpty() { fmt.Println("List is empty") return } fmt.Println("List:") currentNode := list.head for { fmt.Printf("%v -> ", currentNode.data) currentNode = currentNode.next if currentNode == list.head { break } } fmt.Println() } func main() { list := &CircularLinkedList{} list.addFirst(1) list.addLast(2) list.addLast(3) list.addFirst(0) list.display() fmt.Println("Size:", list.getSize()) list.removeFirst() list.removeFirst() list.display() fmt.Println("Size:", list.getSize()) } ``` 输出结果: ``` List: 0 -> 1 -> 2 -> 3 -> Size: 4 List: 2 -> 3 -> Size: 2 ``` 该代码实现了循环单链表的基本功能,包括添加节点、删除节点、获取链表大小和显示链表内容等。在该示例中,循环单链表的头节点和尾节点都是存储在链表对象中的,而不是每个节点中都保存头节点和尾节点的指针。同时,该示例还实现了判断链表是否为空的方法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值