Go 基础函数

前言

慢慢增加。。。

os.Stat(path)

函数原型:

// Stat returns a FileInfo describing the named file.
// If there is an error, it will be of type *PathError.
func Stat(name string) (FileInfo, error) {
	testlog.Stat(name)
	return statNolog(name)
}

描述:
Stat返回描述命名文件的FileInfo。如果有错误,则类型为*PathError。

os.IsNotExist(err)

函数原型:

// IsNotExist returns a boolean indicating whether the error is known to
// report that a file or directory does not exist. It is satisfied by
// ErrNotExist as well as some syscall errors.
//
// This function predates errors.Is. It only supports errors returned by
// the os package. New code should use errors.Is(err, os.ErrNotExist).
func IsNotExist(err error) bool {
	return underlyingErrorIs(err, ErrNotExist)
}

描述:
IsNotExist返回一个布尔值,指示是否已知报告文件或目录不存在的错误。ErrNotExist以及一些系统调用错误都满足了这一要求。

此函数早于errors.Is。它只支持操作系统包返回的错误。新代码应该使用errors.Is(err,os.ErrNotExist)。

os.Mkdir()

函数原型:

// Mkdir creates a new directory with the specified name and permission
// bits (before umask).
// If there is an error, it will be of type *PathError.
func Mkdir(name string, perm FileMode) error {
	if runtime.GOOS == "windows" && isWindowsNulName(name) {
		return &PathError{Op: "mkdir", Path: name, Err: syscall.ENOTDIR}
	}
	longName := fixLongPath(name)
	e := ignoringEINTR(func() error {
		return syscall.Mkdir(longName, syscallMode(perm))
	})

	if e != nil {
		return &PathError{Op: "mkdir", Path: name, Err: e}
	}

	// mkdir(2) itself won't handle the sticky bit on *BSD and Solaris
	if !supportsCreateWithStickyBit && perm&ModeSticky != 0 {
		e = setStickyBit(name)

		if e != nil {
			Remove(name)
			return e
		}
	}

	return nil
}

描述:
Mkdir使用指定的名称和权限位(在umask之前)创建一个新目录。如果有错误,则类型为*PathError。

flag.StringVar

函数原型:

// StringVar defines a string flag with specified name, default value, and usage string.
// The argument p points to a string variable in which to store the value of the flag.
func StringVar(p *string, name string, value string, usage string) {
	CommandLine.Var(newStringValue(value, p), name, usage)
}

函数说明:
StringVar使用指定的名称、默认值和用法字符串定义字符串标志。参数p指向存储标志值的字符串变量。

flag.Parse()

函数定义:

// Parse parses the command-line flags from os.Args[1:]. Must be called
// after all flags are defined and before flags are accessed by the program.
func Parse() {
	// Ignore errors; CommandLine is set for ExitOnError.
	CommandLine.Parse(os.Args[1:])
}

Parse从os.Args[1:]解析命令行标志。必须在定义所有标志之后和程序访问标志之前调用。

os.Getenv()

函数定义:

// Getenv retrieves the value of the environment variable named by the key.
// It returns the value, which will be empty if the variable is not present.
// To distinguish between an empty value and an unset value, use LookupEnv.
func Getenv(key string) string {
	testlog.Getenv(key)
	v, _ := syscall.Getenv(key)
	return v
}

函数描述:
Getenv检索由键命名的环境变量的值。它返回值,如果变量不存在,该值将为空。要区分空值和未设置值,请使用LookupEnv。

fmt.Errorf()

函数定义:

// Errorf formats according to a format specifier and returns the string as a
// value that satisfies error.
//
// If the format specifier includes a %w verb with an error operand,
// the returned error will implement an Unwrap method returning the operand. It is
// invalid to include more than one %w verb or to supply it with an operand
// that does not implement the error interface. The %w verb is otherwise
// a synonym for %v.
func Errorf(format string, a ...interface{}) error {
	p := newPrinter()
	p.wrapErrs = true
	p.doPrintf(format, a)
	s := string(p.buf)
	var err error
	if p.wrappedErr == nil {
		err = errors.New(s)
	} else {
		err = &wrapError{s, p.wrappedErr}
	}
	p.free()
	return err
}

函数说明
Errorf根据格式说明符格式化,并将字符串作为满足错误的值返回。

如果格式说明符包含带有错误操作数的%w谓词,则返回的错误将实现返回操作数的展开方法。包含多个%w谓词或为其提供不实现错误接口的操作数是无效的。%w动词是%v的同义词。
这句话的含义其实是,将错误按照固定的格式进行格式化,并返回。

filepath.Abs()

函数定义:

// Abs returns an absolute representation of path.
// If the path is not absolute it will be joined with the current
// working directory to turn it into an absolute path. The absolute
// path name for a given file is not guaranteed to be unique.
// Abs calls Clean on the result.
func Abs(path string) (string, error) {
	return abs(path)
}

函数说明:
Abs返回路径的绝对表示形式。

如果路径不是绝对路径,它将与当前工作目录合并以将其转换为绝对路径。给定文件的绝对路径名不能保证唯一。

Abs要求对结果进行清理。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值