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
2023-04-19 14:26:15 +08:00

3.8 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
}

ExecCommand

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

Signature:

type (
	Option func(*exec.Cmd)
)
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)

Example:

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

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

GetOsBits

Get current os bits, 32bit or 64bit. return 32 or 64

Signature:

func GetOsBits() int

Example:

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

func main() {
    osBit := system.GetOsBits()
    fmt.Println(osBit)
}