创建HTTP处理程序

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

Creating http handler

问题

我有一个main.go文件,我想将其改为更好的结构(类似于main2.go),因为如果项目增长,新开发人员很难理解代码。

我的想法是创建一个名为handle的文件夹,并在其中放置handle.go文件,其中包含所有的处理方法。我遇到的问题是我不知道如何在handler.go中设置http.HandleFunc("/R1", HandlerOne)http.HandleFunc("/R2", HandlerTwo),并从main2.go中调用它。

main.go

package main

import (
	"fmt"
	"net/http"
)

func HandlerOne(w http.ResponseWriter, req *http.Request) {
	fmt.Println("message one")
}

func HandlerTwo(w http.ResponseWriter, req *http.Request) {
	fmt.Println("message two")
}

func main() {
	http.HandleFunc("/R1", HandlerOne)
	http.HandleFunc("/R2", HandlerTwo)

	err := http.ListenAndServe(":9998", nil)

	if err != nil {
		fmt.Printf("Server failed: ", err.Error())
	}
}

main2.go

package main

import (
	"fmt"
	"net/http"
	"handlers/handle"
)

func main() {
	// 调用handle.go
	handle.SetUpMapping(...)
	err := http.ListenAndServe(":9998", nil)

	if err != nil {
		fmt.Printf("Server failed: ", err.Error())
	}
}

handle.go

package handle

import (
	"fmt"
	"net/http"
)

func HandlerOne(w http.ResponseWriter, req *http.Request) {
	fmt.Println("message one")
}

func HandlerTwo(w http.ResponseWriter, req *http.Request) {
	fmt.Println("message two")
}

func SetUpMapping(...) {
	http.HandleFunc("/R1", HandlerOne)
	http.HandleFunc("/R2", HandlerTwo)
	// 其他映射
	http.HandleFunc("/RN", HandlerN)
}

注意:
感谢@kluyg的回答。我要补充一下,我希望看起来我没有解释得很清楚:

我想在handle.go中创建一个函数,可以在其中放置所有处理函数的映射。例如:

func SetUpMapping(...) {
    http.HandleFunc("/R1", HandlerOne)
    http.HandleFunc("/R2", HandlerTwo)
    
    // 其他映射
    http.HandleFunc("/RN", HandlerN)
}

其中...应该是一个对http的引用,但这是一个包,而在main中应该像代码3那样。

main2.go

package main

import (
    "fmt"
    "net/http"
    "handlers/handle"
)

func main() {
    // 调用handle.go
    handle.SetUpMapping(...) // 我不知道在这里放什么参数
    err := http.ListenAndServe(":9998", nil)

    if err != nil {
        fmt.Printf("Server failed: ", err.Error())
    }
}
英文:

I have main.go and I want changing to better structure(similarly main2.go ) due to if the proyect would grow up, would not be easy interpret for new developers the code.

My idea is create a folder called handle and put in it the file handle.go with all the handles methods, the trouble that I find is that I do not know how to set up http.HandleFunc("/R1", HandlerOne) and http.HandleFunc("/R2", HandlerTwo) in handler.go and call it from main2.go.

main.go

package main

import (
    "fmt"
    "net/http"

)

func HandlerOne(w http.ResponseWriter, req *http.Request) {
    fmt.Println("message one")
}

func HandlerTwo(w http.ResponseWriter, req *http.Request) {
    fmt.Println("message two")
}

func main() {

    http.HandleFunc("/R1", HandlerOne)
    http.HandleFunc("/R2", HandlerTwo)

    err := http.ListenAndServe(":9998", nil)

    if err != nil {
	    fmt.Printf("Server failed: ", err.Error())
    }
}

main2.go

package main

import (
    "fmt"
    "net/http"

)


func main() {

   // Call handle.go

    err := http.ListenAndServe(":9998", nil)

    if err != nil {
	    fmt.Printf("Server failed: ", err.Error())
    }
}

handle.go

package handle

import (
    "fmt"
    "net/http"
)

func HandlerOne(w http.ResponseWriter, req *http.Request) {
    fmt.Println("message one")
 }

func HandlerTwo(w http.ResponseWriter, req *http.Request) {
    fmt.Println("message two")
}

Note:
Thanks @kluyg for your answer. I going to add that i want that seems I do not explain very well:

That I want is create a function in
handle.go where can I can put all the mapping of the handles. i.e some like that

func SetUpMapping(...){
    http.HandleFunc("/R1", HandlerOne)
    http.HandleFunc("/R2", HandlerTwo)
    
   .....  //Others mapping
    http.HandleFunc("/RN", HandlerN)

}

where ... should be one refence to http, but this is one package, and in the main something like code 3

code 3

main2.go

package main

import (
        "fmt"
        "net/http"
        "handlers/handle"

)


func main() {

        // Call handle.go
        handle.SetUpMapping(...) // i dont know what parameter put here
        err := http.ListenAndServe(":9998", nil)

        if err != nil {
                    fmt.Printf("Server failed: ", err.Error())
        }
}

答案1

得分: 1

我拿到了你的代码,并将main2.go放入$GOPATH/handlers文件夹中,将handle.go放入$GOPATH/handlers/handle文件夹中。然后我修改了main2.go的内容如下:

package main

import (
	"fmt"
	"net/http"

	"handlers/handle"
)

func main() {

	// 调用 handle.go

	http.HandleFunc("/R1", handle.HandlerOne)
	http.HandleFunc("/R2", handle.HandlerTwo)

	err := http.ListenAndServe(":9998", nil)

	if err != nil {
		fmt.Printf("服务器启动失败:", err.Error())
	}
}

它编译并正常工作。我相信这就是你要求的。

更新

好的,你可以将以下代码添加到你的handle.go中:

func SetUpMapping() {
    http.HandleFunc("/R1", HandlerOne)
    http.HandleFunc("/R2", HandlerTwo)

    // 其他映射
    http.HandleFunc("/RN", HandlerN)

}

然后在main2.go中:

package main

import (
        "fmt"
        "net/http"
        "handlers/handle"
)


func main() {

        // 调用 handle.go
        handle.SetUpMapping() // 这里不需要任何参数
        err := http.ListenAndServe(":9998", nil)

        if err != nil {
                    fmt.Printf("服务器启动失败:", err.Error())
        }
}
英文:

I took your code and put main2.go into $GOPATH/handlers folder and handle.go in $GOPATH/handlers/handle folder. Then I changed main2.go to be

package main

import (
	"fmt"
	"net/http"

	"handlers/handle"
)

func main() {

	// Call handle.go

	http.HandleFunc("/R1", handle.HandlerOne)
	http.HandleFunc("/R2", handle.HandlerTwo)

	err := http.ListenAndServe(":9998", nil)

	if err != nil {
		fmt.Printf("Server failed: ", err.Error())
	}
}

It compiles and works fine. I believe this is what you asked for.

UPDATE

OK, you can add this to your handle.go

func SetUpMapping() {
    http.HandleFunc("/R1", HandlerOne)
    http.HandleFunc("/R2", HandlerTwo)

    .....  //Others mapping
    http.HandleFunc("/RN", HandlerN)

}

then in main2.go

package main

import (
        "fmt"
        "net/http"
        "handlers/handle"
)


func main() {

        // Call handle.go
        handle.SetUpMapping() // you don't need any parameters here
        err := http.ListenAndServe(":9998", nil)

        if err != nil {
                    fmt.Printf("Server failed: ", err.Error())
        }
}

huangapple
  • 本文由 发表于 2014年4月10日 23:57:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/22993085.html
匿名

发表评论

匿名网友

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

确定