duanqiang7631 2015-01-18 19:38
浏览 58
已采纳

如何使用gdb调试golang代码查看通道内部内容?

i have this code for example

http://play.golang.org/p/9U22NfrXeq

// A concurrent prime sieve

package main

// Send the sequence 2, 3, 4, ... to channel 'ch'.
func Generate(ch chan<- int) {
    for i := 2; ; i++ {
        ch <- i // Send 'i' to channel 'ch'.
    }
}

// Copy the values from channel 'in' to channel 'out',
// removing those divisible by 'prime'.
func Filter(in <-chan int, out chan<- int, prime int) {
    for {
        i := <-in // Receive value from 'in'.
        if i%prime != 0 {
            out <- i // Send 'i' to 'out'.
        }
    }
}

// The prime sieve: Daisy-chain Filter processes.
func main() {
    ch := make(chan int) // Create a new channel.
    go Generate(ch)      // Launch Generate goroutine.
    for i := 0; i < 10; i++ {
        prime := <-ch
        print(prime, "
")
        ch1 := make(chan int)
        go Filter(ch, ch1, prime)
        ch = ch1
    }
}

How to see what's inside channel? For example I want to debug this line:

prime := <-ch

By typing 'p ch' in gdb only prints address of the channel. How can I get contents?

  • 写回答

1条回答 默认 最新

  • dqs86517 2015-01-18 20:02
    关注

    You just have to dereference ch. With a very small program:

    package main
    
    func main() {
        ch := make(chan int, 10)
        ch <- 1
        ch <- 2
        ch <- 4
        <-ch
    }
    

    Debugging:

    (gdb) p *ch
    $1 = struct hchan<int> = {1, 2, 4}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助