Home  >  Article  >  Backend Development  >  golang cookie settings

golang cookie settings

WBOY
WBOYOriginal
2023-05-19 09:02:06855browse

Golang is an efficient, concise, and rapidly developed programming language for building web applications. In most web applications, cookies are used to manage the user session and the state of the website. This article explains how to set and handle cookies in Golang.

1. What is a cookie?

A cookie is a text file that is stored on the computer of a user visiting a website. Cookies are generated by the server and sent to the browser in an HTTP response. The browser stores the cookie on the user's computer and sends the cookie back to the server each time the same website is requested. This allows the server to read the information stored in the cookie and then perform appropriate operations, such as maintaining user session state.

2. Setting cookies

In Golang, setting cookies is very simple. Cookies can be set using the SetCookie method in the net/http package. This method requires a ResponseWriter and a pointer to an http.Cookie structure.

The sample code is as follows:

package main

import (
    "fmt"
    "net/http"
)

func setCookie(w http.ResponseWriter, r *http.Request) {
    cookie := &http.Cookie{
        Name:    "name",
        Value:   "value",
        Expires: time.Now().Add(24 * time.Hour),
    }
    http.SetCookie(w, cookie)
    fmt.Fprintln(w, "Cookie set successfully!")
}

func main() {
    http.HandleFunc("/", setCookie)
    http.ListenAndServe(":8080", nil)
}

In the above code, we define a cookie named "name" with a value of "value" and set its expiration time to 24 hours . Then, use the SetCookie method to write the cookie to the ResponseWriter. Finally, when we access the application in the browser, we will see the cookie in the response header.

3. Read cookies

In Golang, you can use the Cookie method in the Request structure to read cookies. This method returns a pointer to the http.Cookie structure. If the cookie does not exist, returns nil.

The sample code is as follows:

func getCookie(w http.ResponseWriter, r *http.Request) {
    cookie, err := r.Cookie("name")
    if err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }
    fmt.Fprintf(w, "Cookie: %v", cookie.Value)
}

In the above code, we use the Request.Cookie method to retrieve the cookie named "name". If the cookie does not exist, an error response is returned.

4. Delete cookies

In Golang, you can delete cookies using the same method as setting cookies (SetCookie). However, set the cookie's expiration time to a time in the past so that the browser no longer sends the cookie.

The sample code is as follows:

func deleteCookie(w http.ResponseWriter, r *http.Request) {
    cookie := &http.Cookie{
        Name:    "name",
        Value:   "",
        Expires: time.Unix(0, 0),
    }
    http.SetCookie(w, cookie)
    fmt.Fprintln(w, "Cookie deleted successfully!")
}

In the above code, we define an empty cookie named "name" and set its expiration time to the past time. We then write it to the ResponseWriter using the SetCookie method. This will cause the browser to delete this cookie stored on the user's computer.

5. Summary

This article introduces how to set, read and delete cookies in Golang. User session management and state maintenance can be easily implemented in your web application using the methods we provide in this article. Happy programming!

The above is the detailed content of golang cookie settings. 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
Previous article:golang array deleteNext article:golang array delete