1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00
Files
lancet/docs/system_zh-CN.md
2022-03-16 16:18:28 +08:00

3.3 KiB

System

system包含os, runtime, shell command相关函数。

源码:

https://github.com/duke-git/lancet/blob/v1/system/os.go

用法:

import (
    "github.com/duke-git/lancet/system"
)

目录

Documentation文档

IsWindows

检查当前操作系统是否是windows

Signature:

func IsWindows() bool

Example:

import (
	"fmt"
	"github.com/duke-git/lancet/system"
)

func main() {
	isOsWindows := system.IsWindows()
	fmt.Println(isOsWindows)
}

IsLinux

检查当前操作系统是否是linux

Signature:

func IsLinux() bool

Example:

import (
	"fmt"
	"github.com/duke-git/lancet/system"
)

func main() {
	isOsLinux := system.IsLinux()
	fmt.Println(isOsLinux)
}

IsMac

检查当前操作系统是否是macos

Signature:

func IsMac() bool

Example:

import (
	"fmt"
	"github.com/duke-git/lancet/system"
)

func main() {
	isOsMac := system.IsMac
	fmt.Println(isOsMac)
}

GetOsEnv

获取key命名的环境变量的值

Signature:

func GetOsEnv(key string) string

Example:

import (
	"fmt"
	"github.com/duke-git/lancet/system"
)

func main() {
	fooEnv := system.GetOsEnv("foo")
	fmt.Println(fooEnv)
}

SetOsEnv

设置由key命名的环境变量的值

Signature:

func SetOsEnv(key, value string) error

Example:

import (
	"fmt"
	"github.com/duke-git/lancet/system"
)

func main() {
	err := system.SetOsEnv("foo", "foo_value")
	fmt.Println(err)
}

RemoveOsEnv

删除单个环境变量

Signature:

func RemoveOsEnv(key string) error

Example:

import (
	"fmt"
	"github.com/duke-git/lancet/system"
)

func main() {
	err := system.RemoveOsEnv("foo")
	if err != nil {
		fmt.Println(err)
	}
}

CompareOsEnv

获取key命名的环境变量值并与compareEnv进行比较

Signature:

func CompareOsEnv(key, comparedEnv string) bool

Example:

import (
	"fmt"
	"github.com/duke-git/lancet/system"
)

func main() {
	system.SetOsEnv("foo", "foo_value")
	res := system.CompareOsEnv("foo", "foo_value")
	fmt.Println(res) //true
}

CompareOsEnv

使用shell /bin/bash -c(linux) 或 cmd (windows) 执行shell命令

Signature:

func ExecCommand(command string) (stdout, stderr string, err error)

Example:

import (
	"fmt"
	"github.com/duke-git/lancet/system"
)

func main() {
	out, errout, err := system.ExecCommand("ls")
	fmt.Println(out)
	fmt.Println(errout)
	fmt.Println(err)
}