Home  >  Article  >  Backend Development  >  How to use HTTP server function in Go language to serve static files?

How to use HTTP server function in Go language to serve static files?

王林
王林Original
2023-07-31 22:41:181384browse

How to use HTTP server function in Go language to serve static files?

Go is a concise and efficient programming language. It has built-in powerful HTTP server functions, which can easily serve static files. In this article, we will introduce how to use the HTTP server function in the Go language to build a simple static file server, and provide code examples.

First, we need to import the net/http package, which provides related functions and types required to build an HTTP server. Next, we need to define a processor function to handle client requests.

func handler(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, r.URL.Path[1:])
}

In the above code, the http.ServeFile function will send the corresponding file to the client according to the requested path. The w parameter represents the response written to the client, and the r parameter represents the client's request. r.URL.Path[1:] represents the string after removing the first character from the requested path.

Next, we need to call the http.Handle function to bind the processor function to the path.

func main() {
    http.Handle("/", http.HandlerFunc(handler))
    http.ListenAndServe(":8080", nil)
}

In the above code, the http.Handle function binds the processor function to the root path, which means that when the client requests the root path, it will call The handler function is used for processing. http.ListenAndServe The function is used to start the HTTP server at the specified address. The default port number used is 80, and 8080 is specified here.

Finally, we need to run the program in the terminal to start the HTTP server.

$ go run main.go

Now, we can visit http://localhost:8080/ in the browser, and we can see the static files sent by the server to the client.

To summarize, it is very simple to use the HTTP server function in the Go language to serve static files. We only need to define a processor function, bind it to the path, and then start the HTTP server. I hope this article can help you understand how to use the HTTP server function in the Go language to serve static files.

The reference code is as follows:

package main

import (
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, r.URL.Path[1:])
}

func main() {
    http.Handle("/", http.HandlerFunc(handler))
    http.ListenAndServe(":8080", nil)
}

The above is the detailed content of How to use HTTP server function in Go language to serve static files?. 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