Home  >  Article  >  Backend Development  >  How to print header in golang

How to print header in golang

PHPz
PHPzOriginal
2023-05-10 17:36:37872browse

In Go language, printing header can be implemented using the Header type and http.Header() function in the net/http package. The following will introduce the specific operations of how golang prints headers.

First, we need to import the net/http package. This package is a standard library in golang, including the implementation of HTTP client and server.

import (
    "net/http"
    "fmt"
)

Next, we can create an http request object and send the request to a certain URL:

resp, err := http.Get("http://www.example.com")
if err != nil {
    // 请求发生错误
}

Here we send a GET request to the specified URL through the http.Get() function address and store the response in the resp object. Next, we can obtain the Header information through the resp object:

header := resp.Header
fmt.Println(header)

resp.Header is a Header type object, which contains the key-value pairs in the HTTP response header. By calling the fmt.Println() function, we can print out the content in the response header.

In addition to using resp.Header, we can also create a new Header object through the http.Header() function and add key-value pairs to it:

header := http.Header{}
header.Add("Content-Type", "application/json")
header.Add("Authorization", "Bearer xxxxxxxx")
fmt.Println(header)

Here we create a A new Header object, and use the Add() function to add the two key-value pairs "Content-Type" and "Authorization". Next, we can also use the fmt.Println() function to print out the new Header object.

In actual development, we can also use other third-party libraries to process headers. For example, use github.com/golobby/httpheader to generate and parse headers. The library provides rich functions to add, delete, get and set headers. The following is the sample code of the library:

import (
    "github.com/golobby/httpheader"
    "fmt"
)

header := httpheader.New(map[string][]string{
    "Content-Type": {"application/json"},
    "Authorization": {"Bearer xxxxxxxx"},
})
fmt.Println(header.Headers())

Here we create a new Header object by calling the httpheader.New() function and add the two keys "Content-Type" and "Authorization" to it value pair. Calling the Headers() function can convert the Header object into the http.Header type so that HTTP requests can be sent using the net/http package.

Summary:

Printing header is a very simple operation in golang. We can use resp.Header to obtain the response header information and call the fmt.Println() function to print it out; we can also use the http.Header() function to create a new Header object and use the Add() function to add key-value pairs. If you need richer header processing, you can also use third-party libraries such as github.com/golobby/httpheader.

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