Golang-没有选择器的软件包用户

Tah*_*een -1 go

拜托,我已经搜索了很多,但在找不到之后,我正在写,并不是说我没有首先尝试搜索。无法得到正确的答案。我什至试图检查Revel的功能,也无法从那里得到答案。

当我运行该程序时,出现此错误行

./test.go:11: use of package http without selector
Run Code Online (Sandbox Code Playgroud)

这个错误指向我写的下面的行

*http
Run Code Online (Sandbox Code Playgroud)

内部结构

令人困惑的是,使用test和dot我什至无法自动完成VIM。所以我不知道为什么会出错。是不是一定有点像

*(net/http)
Run Code Online (Sandbox Code Playgroud)

或类似的东西 ?

package main

import (
    "fmt"
    "net/http"
)

type HandleHTTP struct {
    *http
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Path is %s", r.URL.Path[1:])

}

func main() {

    test := HandleHTTP{}

    test.http.HandleFunc("/", handler)
    test.http.ListenAndServe(":8080", nil)

}
Run Code Online (Sandbox Code Playgroud)

mko*_*iva 6

如果要让两个或更多实例从不同的端口服务,则需要启动两个或更多服务器。这样的事情也许对您有用吗?

package main

import (
    "fmt"
    "net/http"
)

type HandleHTTP struct {
    http *http.Server
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Path is %s", r.URL.Path[1:])

}

func main() {
    mux1 := http.NewServeMux()
    mux1.HandleFunc("/", handler)
    test1 := HandleHTTP{http:&http.Server{Addr:":8081", Handler:mux1}}

    mux2 := http.NewServeMux()
    mux2.HandleFunc("/", handler)
    test2 := HandleHTTP{http:&http.Server{Addr:":8082", Handler:mux2}}

    // run the first one in a goroutine so that the second one is executed
    go test1.http.ListenAndServe()
    test2.http.ListenAndServe()

}
Run Code Online (Sandbox Code Playgroud)