Golang – 不使用选择器的包的用户

huangapple go评论90阅读模式
英文:

Golang - User of package without selector

问题

请注意,我将为您提供翻译的内容,但不会回答关于翻译的问题。以下是您要翻译的内容:

请,我已经搜索了很多次,但没有找到答案,所以我现在写下来。并不是说我没有尝试过先搜索所有的内容。我找不到正确的答案。我甚至尝试了检查Revel的函数,但也没有找到答案。

当我运行这个程序时,我在以下代码行上得到了错误:

./test.go:11: use of package http without selector

这个错误指向我在结构体中写的下面这行代码:

*http

令人困惑的是,即使在使用VIM时,我也可以得到关于test和dot的自动补全。所以我不知道为什么会出现错误。是不是应该像这样写:

*(net/http)

或者类似于这样的写法?

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)

}
英文:

Please, I searched this a lot and after not been able to find, I am writing and not that I didn't try to search all over first. Couldn't get the right answer. I even tried to check Revel's function and couldn't get the answer from there as well.

When I run this program I get this error for line

./test.go:11: use of package http without selector

This error points at the line below where I have written

*http

inside the struct

Confusing part is that with test and dot I even get auto complete with VIM. So I don't know why is the error. Is it that it has to be somewhat like

*(net/http)

or something like that ?

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)

}

答案1

得分: 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}}

    // 在一个 goroutine 中运行第一个服务器,以便第二个服务器也能执行
    go test1.http.ListenAndServe()
    test2.http.ListenAndServe()
}

这段代码会创建两个服务器实例,分别监听8081和8082端口。每个服务器都使用相同的处理函数handler来处理请求。通过在一个goroutine中运行第一个服务器,可以确保第二个服务器也能被执行。

英文:

If you want to have two or more instances serving from different ports you need to spin up two, or more, server. Would something like this, perhaps, work for you?

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()

}

huangapple
  • 本文由 发表于 2017年3月19日 04:37:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/42879579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定