猿问

如何在一个应用程序中创建多个 http 服务器?

我想在一个 golang 应用程序中创建两个 http 服务器。例子:


    package main


    import (

    "io"

    "net/http"

)


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

    io.WriteString(w, "Hello world one!")

}


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

    io.WriteString(w, "Hello world two!")

}


func main() {

    // how to create two http server instatce? 

    http.HandleFunc("/", helloOne)

    http.HandleFunc("/", helloTwo)

    go http.ListenAndServe(":8001", nil)

    http.ListenAndServe(":8002", nil)

}

如何创建两个 http 服务器实例并为它们添加处理程序?


紫衣仙女
浏览 162回答 1
1回答
随时随地看视频慕课网APP

相关分类

Go
我要回答