resp, err := http.Get("https://example.com") if err != nil { log.Fatal(err) } defer resp.Body.Close() cookies := resp.Cookies() for _, cookie := range cookies { fmt.Printf("Name: %s, Value: %s\n", cookie.Name, cookie.Value) }
resp := http.Response{ StatusCode: http.StatusOK, Body: ioutil.NopCloser(bytes.NewReader([]byte("Hello, world!"))), } cookie := &http.Cookie{ Name: "session_id", Value: "abcd1234", Path: "/", } resp.Header = make(http.Header) resp.Header.Add("Set-Cookie", cookie.String())This code creates a new HTTP response without any cookies. It then creates a new cookie using the http.Cookie type and adds it to the response's header using `Header.Add()` method. The ResponseCookies type is a part of the net/http package in Go.