Documentation ¶
Overview ¶
Package ex implements interfaces which writes output to stdout
Index ¶
Examples ¶
- BodyOf
- BodyWriter.Write
- BodyWriter.WriteHeader
- DumpParts
- DumpParts (With_nice_json)
- HeaderWriter.Write
- HeaderWriter.WriteHeader
- HeadersOf
- JsonOf
- JsonWriter.Write
- JsonWriter.Write (Specific_output)
- JsonWriter.WriteHeader
- JsonWriter.WriteTo
- StatusOf
- StatusWriter.Write
- StatusWriter.WriteHeader
- Tprint
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DumpParts ¶
DumpParts prints each part of a multipart message
Example ¶
buf := bytes.NewBufferString("") body := multipart.NewWriter(buf) body.SetBoundary("--X") w, _ := body.CreatePart(textproto.MIMEHeader{}) w.Write([]byte("Hello")) w, _ = body.CreatePart(textproto.MIMEHeader{}) w.Write([]byte(", world!")) DumpParts(buf, "--X")
Output: Hello, world!
Example (With_nice_json) ¶
buf := bytes.NewBufferString("") body := multipart.NewWriter(buf) body.SetBoundary("--X") header := textproto.MIMEHeader{} header["Content-Type"] = []string{"application/json"} w, _ := body.CreatePart(header) w.Write([]byte(`{"text":"hello","more":"world"}`)) w, _ = body.CreatePart(textproto.MIMEHeader{}) w.Write([]byte("next part")) DumpParts(buf, "--X")
Output: { "text":"hello", "more":"world" } next part
func Print ¶
func Print(any ...interface{})
Print writes any element to stdout.
Example ¶
h := http.Header{} h.Add("Content-Type", "text/plain") h.Add("Abbe", "one") h.Add("Abbe", "two") Print(h)
Output: Abbe: one Abbe: two Content-Type: text/plain
func Tprint ¶
func Tprint(tidy TidyFunc, any ...interface{})
Example ¶
h := http.Header{} h.Add("Content-Type", "text/plain") h.Add("Abbe", "one") h.Add("Abbe", "two") tidy := func(v string) string { return strings.Replace(v, "one", "zero", -1) } Tprint(tidy, h)
Output: Abbe: zero Abbe: two Content-Type: text/plain
Types ¶
type BodyWriter ¶
type BodyWriter struct {
// contains filtered or unexported fields
}
func BodyOf ¶
Example ¶
handler := func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %s!", "world") } handler(BodyOf(http.NewRequest("GET", "/", nil)))
Output: Hello, world!
func NewBodyWriter ¶
func NewBodyWriter() *BodyWriter
func (*BodyWriter) Header ¶
func (w *BodyWriter) Header() http.Header
func (*BodyWriter) Write ¶
func (w *BodyWriter) Write(b []byte) (int, error)
Write the given byte slice to stdout
Example ¶
bodyWriter.Write([]byte("Hello, world!"))
Output: Hello, world!
func (*BodyWriter) WriteHeader ¶
func (w *BodyWriter) WriteHeader(v int)
Example ¶
bodyWriter.WriteHeader(200)
Output:
type HeaderWriter ¶
type HeaderWriter struct {
// contains filtered or unexported fields
}
func HeadersOf ¶
Example ¶
handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "Hello, %s!", "world") } handler(HeadersOf(http.NewRequest("GET", "/", nil)))
Output:
func NewHeaderWriter ¶
func NewHeaderWriter() *HeaderWriter
func (*HeaderWriter) Header ¶
func (w *HeaderWriter) Header() http.Header
func (*HeaderWriter) Write ¶
func (w *HeaderWriter) Write(b []byte) (int, error)
Write the given byte slice to stdout
Example ¶
headerWriter.Write([]byte("body not visible"))
Output:
func (*HeaderWriter) WriteHeader ¶
func (w *HeaderWriter) WriteHeader(v int)
Example ¶
w := NewHeaderWriter() w.Header().Add("Content-type", "text/plain") w.Header().Add("Accept", "*") w.Header().Add("Accept", "application/json") w.Write([]byte("body not visible")) w.Write([]byte("second write does nothing"))
Output: Accept: *; application/json Content-Type: text/plain
type JsonWriter ¶
JsonWriter formats incomming json and implements http.ResponseWriter.
func JsonOf ¶
Example ¶
handler := func(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode( struct { Message string }{"Hello, World!"}) } handler(JsonOf(http.NewRequest("GET", "/", nil)))
Output: { "Message":"Hello, World!" }
func NewJsonWriter ¶
func NewJsonWriter() *JsonWriter
func (*JsonWriter) Header ¶
func (w *JsonWriter) Header() http.Header
func (*JsonWriter) Write ¶
func (w *JsonWriter) Write(b []byte) (int, error)
Write the given byte slice to stdout
Example ¶
json := `{"name":"John","car":{"model":"x,2","plate":"abc\"123"}}` reader := bytes.NewBufferString(json) io.Copy(jsonWriter, reader)
Output: { "name":"John", "car":{ "model":"x,2", "plate":"abc\"123" } }
Example (Specific_output) ¶
json := `{"name":"John","car":{"model":"x,2","plate":"abc\"123"}}` reader := bytes.NewBufferString(json) jsonWriter := NewJsonWriter() jsonWriter.Out = os.Stdout io.Copy(jsonWriter, reader)
Output: { "name":"John", "car":{ "model":"x,2", "plate":"abc\"123" } }
func (*JsonWriter) WriteHeader ¶
func (w *JsonWriter) WriteHeader(v int)
Example ¶
jsonWriter.WriteHeader(200)
Output:
func (*JsonWriter) WriteTo ¶
Write the given byte slice to stdout
Example ¶
json := `{"name":"John","car":{"model":"x,2","plate":"abc\"123"}}` buf := bytes.NewBufferString("") jsonWriter.WriteTo(buf, []byte(json)) fmt.Print(buf.String())
Output: { "name":"John", "car":{ "model":"x,2", "plate":"abc\"123" } }
type StatusWriter ¶
type StatusWriter struct {
// contains filtered or unexported fields
}
func NewStatusWriter ¶
func NewStatusWriter() *StatusWriter
func StatusOf ¶
Example ¶
handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } handler(StatusOf(http.NewRequest("GET", "/", nil)))
Output: 200
func (*StatusWriter) Header ¶
func (w *StatusWriter) Header() http.Header
func (*StatusWriter) Write ¶
func (w *StatusWriter) Write(b []byte) (int, error)
Example ¶
w := NewStatusWriter() w.Write([]byte("Hello, world!"))
Output: 200
func (*StatusWriter) WriteHeader ¶
func (w *StatusWriter) WriteHeader(v int)
Write the given value to stdout
Example ¶
statusWriter.WriteHeader(200)
Output: 200
Click to show internal directories.
Click to hide internal directories.