Home  >  Article  >  Backend Development  >  How to set static in golang

How to set static in golang

WBOY
WBOYOriginal
2023-05-12 22:43:361071browse

During the development process, we often need to deal with static resources, such as CSS files, JavaScript files, etc. In Golang, these static resources can also be easily processed. This article will introduce how to set up static resources in Golang.

1. What are static resources?

Static resources refer to files that have not been processed and parsed on the server side, such as pictures, CSS, JavaScript and other files. These files can be returned directly through user requests. Parse and render to the browser.

2. Use http.FileServer to set static resources

In Golang, you can use http.FileServer to set static resources. http.FileServer provides a simple method to serve files in a specified file directory to HTTP clients. The specific usage is as follows:

package main

import (
    "net/http"
)

func main() {
    http.Handle("/", http.FileServer(http.Dir("./public/")))
    http.ListenAndServe(":8080", nil)
}

In the above code, http.Dir("./public/") indicates the directory where the static resource to be set is located. "/" means providing static resources when setting the access root path. After the setting is completed, you can visit localhost:8080 through the browser to check whether the setting is successful.

3. Use http.StripPrefix to set multiple static resources

If you want to set multiple static resources in the same server, you can use http.StripPrefix to set it. For example, to set static resources in the three directories of js, css, and images, the code is as follows:

package main

import (
    "net/http"
)

func main() {
    http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("./public/js"))))
    http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./public/css"))))
    http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("./public/images"))))
    http.ListenAndServe(":8080", nil)
}

In the above code, http.StripPrefix is ​​used to safely remove the specified prefix string from the access path. For example, if the access path is "/js/main.js", then http.StripPrefix will remove the "/js/" prefix and then access the "./public/js/main.js" file. In this way, a variety of static resources can be set up.

4. Use custom Handler to set static resources

In addition to using http.FileServer and http.StripPrefix, you can also customize Handler to process static resources. For example:

package main

import (
    "net/http"
)

func main() {
    http.HandleFunc("/js/", func(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, "./public"+r.URL.Path)
    })
    http.HandleFunc("/css/", func(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, "./public"+r.URL.Path)
    })
    http.HandleFunc("/images/", func(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, "./public"+r.URL.Path)
    })
    http.ListenAndServe(":8080", nil)
}

In the above code, when accessing the "/js/", "/css/", "/images/" paths, the corresponding Handler will be called and http.ServeFile will be used to provide it. Static resources.

5. Use the third-party library gin to set static resources

If you are using gin in the Golang web framework, you can easily set static resources. For example:

package main

import (
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()
    r.Static("/js", "./public/js")
    r.Static("/css", "./public/css")
    r.Static("/images", "./public/images")
    r.Run(":8080")
}

In the above code, the gin framework is used to set static resources. When accessing the "/js", "/css", and "/images" paths, the corresponding static resources will be provided.

6. Summary

The above is the method of setting static resources in Golang, including using http.FileServer, http.StripPrefix, custom Handler and gin framework, etc. Choosing the appropriate method during development can easily handle static resources and improve development efficiency.

The above is the detailed content of How to set static in golang. 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