mypkg

package module
v0.0.0-...-77c29d4 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2020 License: MIT Imports: 21 Imported by: 0

README

mypkg

错误处理

  • 文件名:error.go
// Err 包含错误代码与错误信息的结构体
type Err struct {
    Code int
    Msg  string
}

// Error 将Err结构体转换为string输出
func (e *Err) Error() string {
    return fmt.Sprintf("code: %d\nmsg: %s", e.Code, e.Msg)
}

// Json 将Err结构体转换为json输出
func (e *Err) Json() string {
    err, _ := json.Marshal(e)
    return string(err)
}

// New 对当前Err重新赋值
func (e *Err) New(code int, msg string) {
    e.Code = code
    e.Msg = msg
}

命令行参数处理(FLAG)

  • 文件名:flag.go
// 显示所有的命令行传入参数
// ShowArgs show all args
func ShowArgs() {
    for i, args := range os.Args {
        fmt.Printf("args[%d]=%s\n", i, args)
    }
}

// 判断flag是否被传入
// IsFlagPassed Is Flag Passed
func IsFlagPassed(name string) bool {
    found := false
    flag.Visit(func(f *flag.Flag) {
        if f.Name == name {
            found = true
        }
    })
    return found
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// OriginalWorkingPath Original Working Path
	OriginalWorkingPath, _ = os.Getwd()
)

Functions

func BubbleSort

func BubbleSort(slice []int) []int

BubbleSort 冒泡排序

func Checksum

func Checksum(mode string, file string) string

Checksum return checksum

func IPGet

func IPGet() map[string]string

IPGet Get the IP address of the physical network adapter, ignore the virtual machine network adapter

func IsFlagPassed

func IsFlagPassed(name string) bool

判断flag是否被传入 IsFlagPassed Is Flag Passed

func ListSearchFirstElement

func ListSearchFirstElement(source *list.List, target string) *list.Element

ListSearchFirstElement Search for the first matching element in the list

func MatchFilter

func MatchFilter(filter []string, s string) bool

MatchFilter 过滤表匹配字符串

func MatchString

func MatchString(pattern string, s string) bool

MatchString 字符串匹配字符串

func PathCreate

func PathCreate(path string) bool

PathCreate Create path if the path not exists

func PathExecFile

func PathExecFile() (string, error)

PathExecFile Return the program running path and error output

func PathExist

func PathExist(path string) bool

PathExist Check if path string exists

func PathWalk

func PathWalk(path string, listPointer interface{}, filter []string, subFolder bool) error

PathWalk 递归将指定路径下文件加入列表(链表或者切片),带过滤器

func ProgramExist

func ProgramExist(programName string, programPath ...interface{}) (bool, string, error)

ProgramExist Check if the program exists in environment variable or input path

func ProgramRealtimeOutput

func ProgramRealtimeOutput(cmd *exec.Cmd) error

ProgramRealtimeOutput Execute commands and get real-time output

func QuickSort

func QuickSort(arr []int) []int

QuickSort 快速排序(递归)

func SelectionSort

func SelectionSort(slice []int) []int

SelectionSort 选择排序

func ShowArgs

func ShowArgs()

显示所有的命令行传入参数 ShowArgs show all args

func SliceClear

func SliceClear(s *[]interface{})

SliceClear 清空切片

func SliceDelete

func SliceDelete(slice *[]string, index int) error

SliceDelete 删除元素

func StringFindInSlice

func StringFindInSlice(slice []string, str string) int

StringFindInSlice find string's position in a slice

func StringTrimSuffix

func StringTrimSuffix(file string) string

StringTrimSuffix remove suffix

func Unzip

func Unzip(archive, target string) error

Unzip unzip("/tmp/report-2015.zip", "/tmp/reports/")

func Zipit

func Zipit(source, target string) error

Zipit zipit("/tmp/documents", "/tmp/backup.zip") or zipit("/tmp/report.txt", "/tmp/report-2015.zip")

Types

type Err

type Err struct {
	Code int
	Msg  string
}

Err 包含错误代码与错误信息的结构体

func (*Err) Error

func (e *Err) Error() string

Error 将Err结构体转换为string输出

Example
var err Err
err.New(404, "can't found")
fmt.Print(err.Error())
Output:

code: 404
msg: can't found

func (*Err) Json

func (e *Err) Json() string

Json 将Err结构体转换为json输出

Example
var err Err
err.New(404, "can't found")
fmt.Print(err.Json())
Output:

{"Code":404,"Msg":"can't found"}

func (*Err) New

func (e *Err) New(code int, msg string)

New 对当前Err重新赋值

Example
var err Err
err.New(404, "can't found")
fmt.Print(err)
Output:

{404 can't found}

Jump to

Keyboard shortcuts

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