Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9056413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:12:04+00:00 2026-06-16T14:12:04+00:00

I am learning Go and am working on this lesson from the GoTours. Here’s

  • 0

I am learning Go and am working on this lesson from the GoTours. Here’s what I have so far.

package main

import (
    "fmt"
    "code.google.com/p/go-tour/tree"
)

// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
    if t != nil {
        Walk(t.Left, ch)
        ch <- t.Value
        Walk(t.Right, ch)
    }
}

func main() {
    var ch chan int = make(chan int)
    go Walk(tree.New(1), ch)
    for c := range ch {
        fmt.Printf("%d ", c)    
    }
}

As you see, I try to test out my Walk function by printing out the values I wrote into a channel. However, I get the following error.

1 2 3 4 5 6 7 8 9 10 throw: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
    main.go:25 +0x85

goroutine 2 [syscall]:
created by runtime.main
    /usr/local/go/src/pkg/runtime/proc.c:221

exit status 2

This error should be expected I think because I never close the channel. However, is there a way I could “catch” this deadlock error and deal with it programmatically?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T14:12:05+00:00Added an answer on June 16, 2026 at 2:12 pm

    Deadlock is similar to a nil pointer deference in that is represents a BUG in your program. This class of error is usually not recoverable for this reason.

    As lbonn mentioned, the problem here is you need to “close(myChan)” your channel. If you do not do this the for-range loop, that the loop will wait for the next element forever.

    You can try something like this:

    func main() {
        var ch chan int = make(chan int)
        go func() {
            Walk(tree.New(1), ch)
            close(ch)
        }()
        for c := range ch {
            fmt.Printf("%d ", c)
        }
    }
    

    If you want to traverse the tree in parallel you will need to make further changes:

    package main
    
    import (
        "code.google.com/p/go-tour/tree"
        "fmt"
        "sync"
    )
    
    // Walk walks the tree t sending all values
    // from the tree to the channel ch.
    func Walk(t *tree.Tree, ch chan int, done *sync.WaitGroup) {
        if t != nil {
            done.Add(2)
            go Walk(t.Left, ch, done) //look at each branch in parallel
            go Walk(t.Right, ch, done)
            ch <- t.Value
        }
        done.Done()
    }
    
    func main() {
        var ch chan int = make(chan int, 64) //note the buffer size
        go func() {
            done := new(sync.WaitGroup)
            done.Add(1)
            Walk(tree.New(1), ch, done)
            done.Wait()
            close(ch)
        }()
        for c := range ch {
            fmt.Printf("%d ", c)
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a learning exercise in expression trees. I have this working code: class
I'm looking through some code for learning purposes. I'm working through this portion of
I'm learning mysql and have been trying to get this working, but an error
I am working on a small project for learning PHP better. This project has
I'm working on a project in C#, it's about E-learning.This project should use plug-ins
I'm working on learning Objective-C/Coaoa, but I've seem to have gotten a bit stuck
So I'm working on learning PDO, and making the transfer from the standard PHP
I have been working on learning the Android NDK the past few days, but
so I have this jQuery script below that works. But since I'm just learning
I'm working on learning GWT (total newb) and have a question regarding the Visualiztion

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.