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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:26:18+00:00 2026-06-13T06:26:18+00:00

I am playing around with Go a bit and I’ve a problem that I

  • 0

I am playing around with Go a bit and I’ve a problem that I am unable to solve.

The following code is the least possible code that reproduces my problem. The goal
of the original code is to delegate http request to goroutines. Each goroutine
does a bit of heavy image calculations and is supposed to respond.

package main

import (
    "fmt"
    "runtime"
    "net/http"
)

func main() {
    http.HandleFunc("/", handle)
    http.ListenAndServe(":8080", nil)
}

func handle(w http.ResponseWriter, r *http.Request) {

    // the idea is to be able to handle several requests
    // in parallel

    // the "go" is problematic
    go delegate(w)
}

func delegate(w http.ResponseWriter) {

    // do some heavy calculations first

    // present the result (in the original code, the image)
    fmt.Fprint(w, "hello")
}

In the case of a go delegate(w) I get no response, without the go it
works out nicely.

Can anyone explain what’s going on? Thanks a lot!

  • 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-13T06:26:20+00:00Added an answer on June 13, 2026 at 6:26 am

    ListenAndServe already launches goroutines to call your handler function, so you shouldn’t do it yourself.

    Here’s the code of the relevant functions from the package source :

    1089    func ListenAndServe(addr string, handler Handler) error {
    1090        server := &Server{Addr: addr, Handler: handler}
    1091        return server.ListenAndServe()
    1092    }
    
    
    1010    func (srv *Server) ListenAndServe() error {
    1011        addr := srv.Addr
    1012        if addr == "" {
    1013            addr = ":http"
    1014        }
    1015        l, e := net.Listen("tcp", addr)
    1016        if e != nil {
    1017            return e
    1018        }
    1019        return srv.Serve(l)
    1020    }
    
    
    1025    func (srv *Server) Serve(l net.Listener) error {
    1026        defer l.Close()
    1027        var tempDelay time.Duration // how long to sleep on accept failure
    1028        for {
    
    1057            go c.serve()
    1058        }
    1059        panic("not reached")
    1060    }
    
    
    579 // Serve a new connection.
    580 func (c *conn) serve() {
    581     defer func() {
    582         err := recover()
    
    669         handler.ServeHTTP(w, w.req)
    

    So your code should simply be

    func handle(w http.ResponseWriter, r *http.Request) {
        // the idea is to be able to handle several requests
        // in parallel
        // do some heavy calculations first
    
        // present the result (in the original code, the image)
        fmt.Fprint(w, "hello")
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am playing around with WPF a bit. Here is the Code that does
I've been playing around with Spring Security a bit and noticed the following oddity.
I was just playing around with bit fields and came across something that I
Hello :) After playing around for a bit, I've come up with the following
I am playing around with ctypes a little bit to massage some C code
Playing around a bit more with jqTouch and am running into a problem with
This surprised me a little bit, but I was playing around with some code
I've been playing around a bit with Minitest, and have found behavior that I
I have been playing around a bit with a fairly simple, home-made search engine,
Was playing around a little bit with the TimeSpan class and I started wondering

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.