lib

package
v0.0.0-...-91e790a Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2022 License: MIT Imports: 5 Imported by: 0

README

工具包

放置一些不好归类的工具

  • http生成按key排序的查询串示例
package main

import (
	"bytes"
	"fmt"

	"github.com/ACV-er/gotools/lib"
)

// 文件为测试用

type GetRoleRequest struct {
	RoleId int    `json:"role_id"`
	Time   int64  `json:"time"`
	Onece  string `json:"onece"`
	Data   struct {
		Name  string `json:"name"`
		Age   int    `json:"age"`
		Sorec struct {
			Name string `json:"name"`
			Age  int    `json:"age"`
		} `json:"sorec"`
	} `json:"data"`
	Sgin string `json:"sgin"`
}

func main() {
	req := &GetRoleRequest{
		RoleId: 1,
		Time:   245,
		Onece:  "dwa4dsf12",
		Data: struct {
			Name  string `json:"name"`
			Age   int    `json:"age"`
			Sorec struct {
				Name string `json:"name"`
				Age  int    `json:"age"`
			} `json:"sorec"`
		}{
			Name: "dwa4dsf12",
			Age:  12,
			Sorec: struct {
				Name string `json:"name"`
				Age  int    `json:"age"`
			}{
				Name: "dwa4dsf12",
				Age:  12,
			},
		},
		Sgin: "sgin",
	}

	str_buf := bytes.NewBufferString("")

	ret, _ := lib.GenArrFromObjectOrderByFieldNameAsc(req)

	for _, v := range ret {
		str_buf.WriteString(v.Key + "=" + lib.AnyToString(v.Value) + "&")
	}

	// 去掉末尾的&
	if str_buf.Len() > 0 {
		str_buf.Truncate(str_buf.Len() - 1)
	}

	str := str_buf.String()
	fmt.Println(str)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyToString

func AnyToString(src interface{}) (ret string)

将任意类型转换为string,对象会先尝试运行String(),如果没有,会尝试变成json

func GenMapFromObject

func GenMapFromObject(src interface{}) (ret map[string]interface{}, err error)

将对象或map生成map[string]interface{}

Types

type SortMap

type SortMap []SortMapNode

func GenArrFromObjectOrderByFieldNameAsc

func GenArrFromObjectOrderByFieldNameAsc(src interface{}) (ret SortMap, err error)

func GenArrFromObjectSortByFieldName

func GenArrFromObjectSortByFieldName(src interface{}, key_cmp func(string, string) bool) (ret SortMap, err error)

type SortMapNode

type SortMapNode struct {
	Key   string
	Value interface{}
}

type Stringer

type Stringer interface {
	String() string
}

Jump to

Keyboard shortcuts

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