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 8814203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:03:17+00:00 2026-06-14T04:03:17+00:00

I started to learn go language days ago. When I tried to start writing

  • 0

I started to learn go language days ago. When I tried to start writing some fun codes, I am stuck by a strange behavior.

package main

import "fmt"

func recv(value int) {
    if value < 0 {
        return
    }

    fmt.Println(value)
    go recv(value-1)
}

func main() {
    recv(10)
}

when I run the above code, only 10 is printed. When I remove the go before the call to recv, 10 to 0 are printed out. I believe I am misusing go routine here, but I can not understand why it failed start a go routine this way.

  • 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-14T04:03:19+00:00Added an answer on June 14, 2026 at 4:03 am

    When the main function returns, Go will not wait for any still existing goroutines to finish but instead just exit.

    recv will return to main after the first “iteration” and because main has nothing more to do, the program will terminate.

    One solution to this problem is to have a channel that signals that all work is done, like the following:

    package main
    
    import "fmt"
    
    func recv(value int, ch chan bool) {
        if value < 0 {
            ch <- true
            return
        }
    
        fmt.Println(value)
        go recv(value - 1, ch)
    }
    
    func main() {
        ch := make(chan bool)
        recv(10, ch)
    
        <-ch
    }
    

    Here, recv will send a single boolean before returning, and main will wait for that message on the channel.

    For the logic of the program, it does not matter what type or specific value you use. bool and true are just a straightforward example. If you want to be more efficient, using a chan struct{} instead of a chan bool will save you an additional byte, since empty structs do not use any memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've started to learn OO programming, but using the PHP language with the help
I recently started to learn how to use openCL to speed up some part
I've recently (4 days) started to learn C++ coming from C / Java background.
I've just started to learn the long-heard python language. I've been working with C
I've been doing some OCaml programming lately to learn the language and to get
I have started to learn c language but the problem is that I'm confused
Just started to learn C language. I have a pointer array int *parr and
I started to learn ruby programming language, i want to output my ruby scripts
I started to learn OO a few days back, I'm quite OK with procedural
I've started programming in Google's Go Language, and the package I'm attempting to write

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.