···
func main() {
TR := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
}
CLIENT := &http.Client{Transport: TR}
server01 := &http.Server{
Addr: ":8080",
Handler: router01(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
server02 := &http.Server{
Addr: ":8081",
Handler: router02(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
g.Go(func() error {
// 下面这句是能看到输出的
log.Println("start server01")
err := server01.ListenAndServe()
//下面这句就看不到输出了。
// 这段代码是想在下面在server01启动以后,等待5秒钟(等server02启动)后,在server01里向server02发起一个请求
log.Println("server01 started")
if err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
//等待5秒
time.Sleep(5 * time.Second)
log.Println("Begin reg")
//向server01发起请求
resp, err := CLIENT.PostForm("http://localhost:8081/reg",
url.Values{"sid": {"1234567"}, "sip": {"127.0.0.1"}})
if err != nil {
log.Println("reg error")
}
defer resp.Body.Close()
//body, err := ioutil.ReadAll(resp.Body)
return err
})
g.Go(func() error {
err := server02.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
return err
})
if err := g.Wait(); err != nil {
log.Fatal(err)
}
}
···
有疑问加站长微信联系(非本文作者)