httpheader

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2017 License: MIT Imports: 6 Imported by: 1

README

go-httpheader

go-httpheader is a Go library for encoding structs into Header fields.

Build Status Go Report Card GoDoc

install

go get -u bitbucket.org/mozillazg/go-httpheader

usage

package main

import (
	"fmt"
	"net/http"

	"bitbucket.org/mozillazg/go-httpheader"
)

func main() {
	type Options struct {
		hide         string
		ContentType  string `header:"Content-Type"`
		Length       int
		XArray       []string `header:"X-Array"`
		TestHide     string   `header:"-"`
		IgnoreEmpty  string   `header:"X-Empty,omitempty"`
		IgnoreEmptyN string   `header:"X-Empty-N,omitempty"`
		CustomHeader http.Header
	}

	opt := Options{
		hide:         "hide",
		ContentType:  "application/json",
		Length:       2,
		XArray:       []string{"test1", "test2"},
		TestHide:     "hide",
		IgnoreEmptyN: "n",
		CustomHeader: http.Header{
			"X-Test-1": []string{"233"},
			"X-Test-2": []string{"666"},
		},
	}
	h, _ := httpheader.Header(opt)
	fmt.Printf("%#v", h)
}

Documentation

Overview

Package httpheader is a Go library for encoding structs into Header fields.

Index

Examples

Constants

View Source
const Version = "0.2.0"

Version ...

Variables

This section is empty.

Functions

func Header(v interface{}) (http.Header, error)

Header returns the http.Header encoding of v.

Example
type Options struct {
	ContentType  string `header:"Content-Type"`
	Length       int
	XArray       []string `header:"X-Array"`
	TestHide     string   `header:"-"`
	IgnoreEmpty  string   `header:"X-Empty,omitempty"`
	IgnoreEmptyN string   `header:"X-Empty-N,omitempty"`
	CustomHeader http.Header
}

opt := Options{
	ContentType:  "application/json",
	Length:       2,
	XArray:       []string{"test1", "test2"},
	TestHide:     "hide",
	IgnoreEmptyN: "n",
	CustomHeader: http.Header{
		"X-Test-1": []string{"233"},
		"X-Test-2": []string{"666"},
	},
}
h, _ := httpheader.Header(opt)
fmt.Println(h["Content-Type"])
fmt.Println(h["Length"])
fmt.Println(h["X-Array"])
_, ok := h["TestHide"]
fmt.Println(ok)
_, ok = h["X-Empty"]
fmt.Println(ok)
fmt.Println(h["X-Empty-N"])
fmt.Println(h["X-Test-1"])
fmt.Println(h["X-Test-2"])
Output:

[application/json]
[2]
[test1 test2]
false
false
[n]
[233]
[666]

Types

type Encoder

type Encoder interface {
	EncodeHeader(key string, v *http.Header) error
}

Encoder ...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL