dqpfkzu360216 2017-03-18 20:37
浏览 300
已采纳

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

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条回答 默认 最新

  • druzuz321103 2017-03-18 21:42
    关注

    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()
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable