multiple response.WriteHeader calls in really simple example?

后端 未结 6 520
慢半拍i
慢半拍i 2020-12-10 10:43

I have the most basic net/http program that I\'m using to learn the namespace in Go:

package main

import (
    \"fmt\"
    \"log\"
    \"net/http\"
)

func          


        
6条回答
  •  清歌不尽
    2020-12-10 11:28

    Yes, use HandleIndex(w, r) instead of go HandleIndex(w, r) will fix your issue, I think you have already figured that out.

    The reason is simple, when handling multiple requests at the same time, the http server will start multiple goroutines, and your handler function will be called separately in each of the goroutines without blocking others. You don't need to start your own goroutine in the handler, unless you practically need it, but that will be another topic.

提交回复
热议问题