Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Go中创建多个http服务器实例不起作用_Go - Fatal编程技术网

在Go中创建多个http服务器实例不起作用

在Go中创建多个http服务器实例不起作用,go,Go,我正在尝试在go lang应用程序中创建2个HTTP服务器,我就是这样尝试实现的: package main import ( "net/http" ) func main() { server := http.Server{ Addr: ":9000", //Handler: http.HandleFunc("/", hello) } server.ListenAndServe() server2 :=

我正在尝试在go lang应用程序中创建2个HTTP服务器,我就是这样尝试实现的:

package main

import (
    "net/http"
)

func main() {

    server := http.Server{
        Addr:    ":9000",
        //Handler:  http.HandleFunc("/", hello)
    }
    server.ListenAndServe()


    server2 := http.Server{
        Addr:    ":8000",
        //Handler:  http.HandleFunc("/", hello)
    }
    server2.ListenAndServe()

}
我遇到的问题是当我去浏览器请求
http://localhost:9000/
它会去,但当我向
http://localhost:8000/
我得到“无法访问站点”。为什么我不能在Go中创建HTTP服务器的实例?

就像我们说的那样,
ListendServe
正在阻塞,因此第一个服务器启动,但随后不继续第二个调用。解决这个问题的一个简单方法是在一个类似goroutine的goroutine中启动
server

func main() {

    server := http.Server{
        Addr:    ":9000",
        //Handler:  http.HandleFunc("/", hello)
    }
    go server.ListenAndServe()


    server2 := http.Server{
        Addr:    ":8000",
        //Handler:  http.HandleFunc("/", hello)
    }
    server2.ListenAndServe()

}

http.Server.listendandserve
在Go例程接受连接时阻止它。@那么是否可以创建到实例?另请参阅