# System | Lancet
一个强大的Go语言工具函数库

Skip to content
本页内容

# System ​

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

## 源码: ​

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

## 用法: ​

go

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

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

## 目录 ​

- IsWindows
- IsLinux
- IsMac
- GetOsEnv
- SetOsEnv
- RemoveOsEnv
- CompareOsEnv
- ExecCommand
- GetOsBits

## 文档 ​

### IsWindows ​

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

函数签名:

go

    func IsWindows() bool

    func IsWindows() bool

示例:运行

go

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

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

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

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

### IsLinux ​

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

函数签名:运行

go

    func IsLinux() bool

    func IsLinux() bool

示例:

go

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

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

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

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

### IsMac ​

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

函数签名:

go

    func IsMac() bool

    func IsMac() bool

示例:运行

go

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

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

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

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

### GetOsEnv ​

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

函数签名:

go

    func GetOsEnv(key string) string

    func GetOsEnv(key string) string

示例:运行

go

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

    func main() {
        err := system.SetOsEnv("foo", "abc")
        result := system.GetOsEnv("foo")

        fmt.Println(err)
        fmt.Println(result)
        // Output:
        // <nil>
        // abc
    }

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

    func main() {
        err := system.SetOsEnv("foo", "abc")
        result := system.GetOsEnv("foo")

        fmt.Println(err)
        fmt.Println(result)
        // Output:
        // <nil>
        // abc
    }

### SetOsEnv ​

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

函数签名:

go

    func SetOsEnv(key, value string) error

    func SetOsEnv(key, value string) error

示例:运行

go

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

    func main() {
        err := system.SetOsEnv("foo", "abc")
        result := system.GetOsEnv("foo")

        fmt.Println(err)
        fmt.Println(result)
        // Output:
        // <nil>
        // abc
    }

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

    func main() {
        err := system.SetOsEnv("foo", "abc")
        result := system.GetOsEnv("foo")

        fmt.Println(err)
        fmt.Println(result)
        // Output:
        // <nil>
        // abc
    }

### RemoveOsEnv ​

删除单个环境变量

函数签名:

go

    func RemoveOsEnv(key string) error

    func RemoveOsEnv(key string) error

示例:运行

go

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

    func main() {
        err1 := system.SetOsEnv("foo", "abc")
        result1 := GetOsEnv("foo")

        err2 := system.RemoveOsEnv("foo")
        result2 := GetOsEnv("foo")

        fmt.Println(err1)
        fmt.Println(err2)
        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // <nil>
        // <nil>
        // abc
        //
    }

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

    func main() {
        err1 := system.SetOsEnv("foo", "abc")
        result1 := GetOsEnv("foo")

        err2 := system.RemoveOsEnv("foo")
        result2 := GetOsEnv("foo")

        fmt.Println(err1)
        fmt.Println(err2)
        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // <nil>
        // <nil>
        // abc
        //
    }

### CompareOsEnv ​

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

函数签名:

go

    func CompareOsEnv(key, comparedEnv string) bool

    func CompareOsEnv(key, comparedEnv string) bool

示例:运行

go

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

    func main() {
        err := system.SetOsEnv("foo", "abc")
        if err != nil {
            return
        }

        result := system.CompareOsEnv("foo", "abc")

        fmt.Println(result)

        // Output:
        // true
    }

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

    func main() {
        err := system.SetOsEnv("foo", "abc")
        if err != nil {
            return
        }

        result := system.CompareOsEnv("foo", "abc")

        fmt.Println(result)

        // Output:
        // true
    }

### ExecCommand ​

执行shell命令，返回命令的stdout和stderr字符串，如果出现错误，则返回错误。参数`command`是一个完整的命令字符串，如ls-a（linux），dir（windows），ping 127.0.0.1。在linux中，使用/bin/bash-c执行命令，在windows中，使用powershell.exe执行命令。

函数签名:

go

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

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

示例:运行

go

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

    func main() {
        // linux or mac
        stdout, stderr, err := system.ExecCommand("ls")
        fmt.Println("std out: ", stdout)
        fmt.Println("std err: ", stderr)
        assert.Equal("", stderr)

        // windows
        stdout, stderr, err = system.ExecCommand("dir")
        fmt.Println("std out: ", stdout)
        fmt.Println("std err: ", stderr)

        // error command
        stdout, stderr, err = system.ExecCommand("abc")
        fmt.Println("std out: ", stdout)
        fmt.Println("std err: ", stderr)
        if err != nil {
            fmt.Println(err.Error())
        }
    }

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

    func main() {
        // linux or mac
        stdout, stderr, err := system.ExecCommand("ls")
        fmt.Println("std out: ", stdout)
        fmt.Println("std err: ", stderr)
        assert.Equal("", stderr)

        // windows
        stdout, stderr, err = system.ExecCommand("dir")
        fmt.Println("std out: ", stdout)
        fmt.Println("std err: ", stderr)

        // error command
        stdout, stderr, err = system.ExecCommand("abc")
        fmt.Println("std out: ", stdout)
        fmt.Println("std err: ", stderr)
        if err != nil {
            fmt.Println(err.Error())
        }
    }

### GetOsBits ​

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

函数签名:

go

    func GetOsBits() int

    func GetOsBits() int

示例:运行

go

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

    func main() {
        osBit := system.GetOsBits()
        fmt.Println(osBit) // 32 or 64
    }

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

    func main() {
        osBit := system.GetOsBits()
        fmt.Println(osBit) // 32 or 64
    }