utils

package
v0.0.0-...-d9d0c30 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2022 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

* @Author: soso * @Date: 2022-01-12 15:58:23 * @LastEditTime: 2022-01-25 12:48:20 * @LastEditors: Please set LastEditors * @Description: 文件锁操作 * @FilePath: /file-sync/utils/flock/flock.go

* @Author: your name * @Date: 2022-02-09 13:18:12 * @LastEditTime: 2022-02-09 17:28:57 * @LastEditors: Please set LastEditors * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @FilePath: /go-mesh-sync/go-utils/utils/map.go

* @Author: soso * @Date: 2022-03-15 11:22:37 * @LastEditTime: 2022-03-15 13:35:35 * @LastEditors: Please set LastEditors * @Description: 网络相关 * @FilePath: /go-utils/utils/net.go

* @Author: soso * @Date: 2022-03-09 16:46:07 * @LastEditTime: 2022-03-09 16:58:36 * @LastEditors: Please set LastEditors * @Description: 获取工作路径 * @FilePath: /go-utils/utils/path.go

* @Author: soso * @Date: 2022-01-20 18:29:37 * @LastEditTime: 2022-01-26 14:57:37 * @LastEditors: Please set LastEditors * @Description: soso * @FilePath: /file-sync/utils/time.go

* @Author: soso * @Date: 2022-01-05 18:55:23 * @LastEditTime: 2022-03-15 11:23:14 * @LastEditors: Please set LastEditors * @Description: 通用工具 * @FilePath: /file-sync/utils/utils.go

Index

Constants

View Source
const (
	BufSize = 8 * 1024
)

Variables

This section is empty.

Functions

func AppendFile

func AppendFile(filename string, buf []byte) (err error)

*

  • @description: 追加方式添加内容
  • @param {string} filename
  • @param {[]byte} buf
  • @return {*}

func AskForConfirmation

func AskForConfirmation(s string) bool

*

  • @description: 确认指令
  • @param {string} s
  • @return {*}

func B2s

func B2s(b []byte) string

b2s converts byte slice to a string without memory allocation. See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ .

Note it may break if string and/or slice header will change in the future go versions.

func CheckIsHidden

func CheckIsHidden(file os.FileInfo) bool

func ClearBlock

func ClearBlock(filename string, offset, size int64) (nn int64, err error)

清除文件的一块

func ClientIP

func ClientIP(r *http.Request) string

func ClientPublicIP

func ClientPublicIP(r *http.Request) string

ClientPublicIP 尽最大努力实现获取客户端公网 IP 的算法。 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。

func CreateEmptyFile

func CreateEmptyFile(filePath string, size int64) error

*

  • 创建一个固定大小的0填充文件

func Difference

func Difference(slice1, slice2 []string) []string

求差集 slice1-并集

func ExecCommand

func ExecCommand(commandName string, arg ...string) ([]string, error)

*

  • @description: 执行命令
  • @param {string} commandName
  • @param {...string} arg
  • @return {*}

func ExecDir

func ExecDir() (execdir string)

执行文件所在目录

func FileCropBlock

func FileCropBlock(filename string, offset, size int64) error

func FileExist

func FileExist(filePath string) (fs.FileInfo, error)

func FileReadToEnd

func FileReadToEnd(filename string, offset int64) (data []byte, err error)

文件读取到最后

func FileReplaceBlock

func FileReplaceBlock(filename string, offset, size int64, newBytes []byte) error

替换文件中的某一块

func FindInIntSlice

func FindInIntSlice(a []int, x int) int

func FindInStrSlice

func FindInStrSlice(a []string, x string) int

func GetAllFiles

func GetAllFiles(pathname string) (files []string)

*

  • @description: 递归罗列文件夹内所有内容(含子目录)
  • @param {string} pathname
  • @return {*}

func GetClientIP

func GetClientIP() (ip string, err error)

通过本地InterfaceAddrs得到IP,绑定多IP情况下会出问题

func GetCurrentAbPath

func GetCurrentAbPath() string

当前执行文件目录

func GetFileContentType

func GetFileContentType(out *os.File) (string, error)

文件 ContentType

func GetFileType

func GetFileType(fSrc []byte) string

用文件前面几个字节来判断 fSrc: 文件字节流(就用前面几个字节)

func GetIpByLocalDns

func GetIpByLocalDns(dnsIp string) (ip string, err error)

通过本地dns的方式得到IP

func GetIpBySubnet

func GetIpBySubnet(subnet string) (ip string, err error)

通过提供子网段的方式获得ip

func GetLocalIP

func GetLocalIP() string

*

  • @description: 以两种方式获得本地IP
  • @param {*}
  • @return {*}

func GetLocalIPOnce

func GetLocalIPOnce() string

*

  • @description: 以两种方式获得本地IP
  • @param {*}
  • @return {*}

func GetOutBoundIP

func GetOutBoundIP() (ip string, err error)

通过UDP方式获取本地ip

func HasLocalIP

func HasLocalIP(ip net.IP) bool

HasLocalIP 检测 IP 地址是否是内网地址

func HasLocalIPddr

func HasLocalIPddr(ip string) bool

HasLocalIPddr 检测 IP 地址字符串是否是内网地址

func HashFileMd5

func HashFileMd5(filename string) (md5str string, err error)

获取文件的md5码

func HashLargeFileMD5

func HashLargeFileMD5(path string) string

获取大文件的Md5值

func HideFile

func HideFile(filename string) error

func Intersect

func Intersect(slice1, slice2 []string) []string

求交集

func LastSecOfTheDayBeforeYesterday

func LastSecOfTheDayBeforeYesterday(tm time.Time) time.Time

func MyCaller

func MyCaller() string

MyCaller returns the caller of the function that called it :)

func OnInternet

func OnInternet() bool

func OpenAppendFile

func OpenAppendFile(filename string, autoCreate bool) (f *os.File, err error)

func OpenRDonlyFile

func OpenRDonlyFile(filename string) (f *os.File, err error)

func OpenWRonlyFile

func OpenWRonlyFile(filename string, autoCreate bool) (f *os.File, err error)

func RelativePath

func RelativePath(path1, path2 string) (string, error)

func RunCommand

func RunCommand(out chan string, name string, arg ...string) error

RunCommand run shell

func S2b

func S2b(s string) (b []byte)

s2b converts string to a byte slice without memory allocation.

Note it may break if string and/or slice header will change in the future go versions.

func StringMd5

func StringMd5(val []byte) []byte

func TimeFromString

func TimeFromString(val string) time.Time

func ToMap

func ToMap(in interface{}, tagName string) (map[string]interface{}, error)

ToMap 结构体转为Map[string]interface{}

func TouchAFile

func TouchAFile(filePath string) error

*

  • @description: 创建一个空文件
  • @param {string} filePath
  • @return {*}

func Union

func Union(slice1, slice2 []string) []string

求并集

func WriteFile

func WriteFile(filename string, data []byte, perm fs.FileMode) error

*

  • @description: 使用系统锁,写入文件
  • @param {string} filename
  • @param {[]byte} data
  • @param {fs.FileMode} perm
  • @return {*}

Types

type Bar

type Bar struct {
	// contains filtered or unexported fields
}

func (*Bar) NewOption

func (bar *Bar) NewOption(start, total int64)

func (*Bar) NewOptionWithGraph

func (bar *Bar) NewOptionWithGraph(start, total int64, graph string)

func (*Bar) Play

func (bar *Bar) Play(cur int64)

type Flock

type Flock struct {
	// contains filtered or unexported fields
}

func FlockCreate

func FlockCreate(file *os.File) (f *Flock, e error)

创建文件锁,配合 defer f.Release() 来使用

func (*Flock) Lock

func (f *Flock) Lock(nb bool) (e error)

上锁,配合 defer f.Unlock() 来使用

func (*Flock) Release

func (f *Flock) Release()

释放文件锁

func (*Flock) Unlock

func (f *Flock) Unlock()

解锁

Jump to

Keyboard shortcuts

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