response

package
v1.16.1 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2023 License: MIT Imports: 2 Imported by: 0

README

API

Response

type Response struct {
    StatusCode int    `json:"status_code"`
    Message    string `json:"message,omitempty"`
    Body       any    `json:"body,omitempty"`
}

判断当前是否为异常响应

func (r Response) IsError() (string, bool)

判断当前是否为正常响应

func (r Response) IsNormal() bool

将当前响应体格式化为 json

func (r Response) Dump() []byte

API

生成正常响应

此时 status_code 为 200,message 无值

func OK(body any) *Response

生成警告响应

此时 status_code 为 500,message 有值

func Warn(msg string) *Response

生成错误响应

status_code 为 500,message 有值

func Error(err error) *Response

生成响应

func Build(code int, msg string, body any) *Response

NoContent

变量,不是方法

status_code 为 204,message 无值

BadRequest

变量,不是方法

status_code 为 400,message 无值

示例

package main

import (
	"fmt"
	"net/http"

	"github.com/o8x/jk/response"
)

func main() {
	fmt.Println(string(response.OK([]string{"hello"}).Dump()))
	fmt.Println(string(response.Warn("warn").Dump()))
	fmt.Println(string(response.Error(fmt.Errorf("error")).Dump()))
	fmt.Println(string(response.Build(http.StatusOK, "", "hello").Dump()))

	fmt.Println(string(response.NoContent.Dump()))
	fmt.Println(response.NoContent.IsError())
	fmt.Println(response.NoContent.IsNormal())

	fmt.Println(string(response.BadRequest.Dump()))
	fmt.Println(response.BadRequest.IsError())
	fmt.Println(response.BadRequest.IsNormal())
} 

运行它

> go run .
{"status_code":200,"body":["hello"]}
{"status_code":500,"message":"warn"}
{"status_code":500,"message":"error"}
{"status_code":200,"body":"hello"}
{"status_code":204}
 false
true
{"status_code":400}
 false
false 

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NoContent = &Response{
		StatusCode: http.StatusNoContent,
	}

	BadRequest = &Response{
		StatusCode: http.StatusBadRequest,
	}
)

Functions

This section is empty.

Types

type Response

type Response struct {
	StatusCode int    `json:"status_code"`
	Message    string `json:"message,omitempty"`
	Body       any    `json:"body,omitempty"`
}

func Build

func Build(code int, msg string, body any) *Response

func Error

func Error(err error) *Response

func OK

func OK(body any) *Response

func Warn

func Warn(msg string) *Response

func (Response) Dump

func (r Response) Dump() []byte

func (Response) IsError

func (r Response) IsError() (string, bool)

func (Response) IsNormal

func (r Response) IsNormal() bool

Jump to

Keyboard shortcuts

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