# System

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

# 源码:

# 用法:

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

# Documentation 文档

# IsWindows

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

Signature:

func IsWindows() bool

Example:

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

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

# IsLinux

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

Signature:

func IsLinux() bool

Example:

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

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

# IsMac

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

Signature:

func IsMac() bool

Example:

import (
	"fmt"
	"github.com/duke-git/lancet/v2/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/v2/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/v2/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/v2/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/v2/system"
)

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

# ExecCommand

使用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/v2/system"
)

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

# GetOsBits

获取当前操作系统位数,返回32或64

函数签名:

func GetOsBits() int

例子:

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

func main() {
	osBit := system.GetOsBits()
	fmt.Println(osBit)
}
最后更新时间: 2022/9/29 下午1:53:14