Home  >  Article  >  Backend Development  >  How to set header in Golang

How to set header in Golang

PHPz
PHPzOriginal
2023-04-03 09:17:152545browse

Golang is an efficient programming language that has become the first choice for many people. In Golang, setting header is a very basic operation, and this function is often used by everyone. So, how to set header in Golang?

In Golang, to set the header, we need to use the Header type in the http package. The Header type represents the HTTP message header, which can be used to add or modify the header information of an HTTP request or response. The Header type is a map type, the key is a string type, and the value can be a slice of string type. Here is an example:

package main

import (
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
        w.Header().Set("Content-Type", "text/plain")
        w.Write([]byte("Hello, world!"))
    })

    http.ListenAndServe(":8080", nil)
}

In the above code, we define a handler function that uses the HandleFunc function in the http package. The HandleFunc function can be used to register a handler function that will be called when the browser accesses the root directory of the website. In the function body, we use ResponseWriter and Request parameters to handle HTTP requests and responses. The w.Header().Set() method is used to set the Content-Type header information, which tells the browser that the content we are sending is plain text rather than HTML. Finally, we use w.Write() to send a string to the browser.

In addition to the Set method, the Header type also provides the Add method and Del method. The Add method is used to add a header information. If the header information already exists, it will be appended after the original header information. The Del method is used to delete a header information. If the header information does not exist, the method call will be ignored.

The following is an example that demonstrates how to use the Add method and Del method to delete and add Http header information:

package main

import (
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
        w.Header().Add("Cache-Control", "no-cache")
        w.Header().Add("Cache-Control", "max-age=0")

        w.Write([]byte("Hello, world!"))
    })

    http.HandleFunc("/deleteheader", func(w http.ResponseWriter, r *http.Request){
        w.Header().Del("Cache-Control")

        w.Write([]byte("Header deleted"))
    })

    http.ListenAndServe(":8080", nil)
}

In the above code, we use the Add method to add Http header information information. Since the Add method can add multiple header information with the same name, we added two values ​​​​for the Cache-Control header information. In the /deleteheader route, we use the Del method to delete the Cache-Control header information.

Summary:

In Golang, you can use the Header type in the http package to set header information. We can use the Set method to set header information, the Add method to add header information, and the Del method to delete header information. If header information with the same name exists, the Add method will append it after the original header information. The Del method will delete all header information with the same name.

I hope this article will be helpful to you, go and try it!

The above is the detailed content of How to set header 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