Home  >  Article  >  Backend Development  >  Use the net/http.HandleFunc function in the Go language documentation to implement HTTP routing processing

Use the net/http.HandleFunc function in the Go language documentation to implement HTTP routing processing

王林
王林Original
2023-11-03 17:05:251312browse

Use the net/http.HandleFunc function in the Go language documentation to implement HTTP routing processing

Use the net/http.HandleFunc function in the Go language documentation to implement HTTP routing processing

The Go language provides a rich network programming library, the most commonly used of which isnet/http package. This package provides a convenient method http.HandleFunc that can be used to handle HTTP routing.

HTTP routing refers to calling different processing functions based on different URLs. In the Go language, this function can be implemented through the http.HandleFunc function, which accepts two parameters: routing path and processing function.

The following is a sample code that shows how to use the http.HandleFunc function to handle HTTP routing:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    // 注册路由处理函数
    http.HandleFunc("/", homeHandler)
    http.HandleFunc("/about", aboutHandler)
    http.HandleFunc("/contact", contactHandler)

    // 启动HTTP服务
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("启动HTTP服务失败:", err)
    }
}

// 路由处理函数
func homeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "欢迎访问首页")
}

func aboutHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "关于我们")
}

func contactHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "联系我们")
}

In this sample code, we define three routes Processing functions: homeHandler, aboutHandler and contactHandler, respectively corresponding to different URL paths. Then use the http.HandleFunc function to register these processing functions on the corresponding routing path.

In the main function, we use the http.ListenAndServe function to start an HTTP service listening on port 8080. This function will block until an error occurs or it is stopped manually.

When an HTTP request arrives, the Go language's net/http package will call the corresponding processing function according to the requested URL path. In the processing function, we can write the content of the HTTP response through the http.ResponseWriter parameter.

In the above example, when accessing the root path /, the homeHandler function will be called, which writes "Welcome to the home page to the client "; When accessing /about, the aboutHandler function will be called, which writes "About Us" to the client; When accessing /contact, the contactHandler function will be called, which writes "Contact Us" to the client.

In this way, we can process different logic according to different URL paths to achieve more flexible HTTP routing processing.

To summarize, HTTP routing processing can be easily implemented using the net/http.HandleFunc function of the Go language. We only need to define different routing processing functions and register them on the corresponding routing paths. This method is very simple, flexible, and suitable for web applications of all sizes.

The above is the detailed content of Use the net/http.HandleFunc function in the Go language documentation to implement HTTP routing processing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn