4.2 KiB
System
Package system contains some functions about os, runtime, shell command.
Source:
Usage:
import (
"github.com/duke-git/lancet/v2/system"
)
Index
Documentation
IsWindows
Check if current os is windows.
Signature:
func IsWindows() bool
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/system"
)
func main() {
isOsWindows := system.IsWindows()
fmt.Println(isOsWindows)
}
IsLinux
Check if current os is linux.
Signature:
func IsLinux() bool
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/system"
)
func main() {
isOsLinux := system.IsLinux()
fmt.Println(isOsLinux)
}
IsMac
Check if current os is macos.
Signature:
func IsMac() bool
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/system"
)
func main() {
isOsMac := system.IsMac
fmt.Println(isOsMac)
}
GetOsEnv
Gets the value of the environment variable named by the 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
Sets the value of the environment variable named by the 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
Remove a single environment variable.
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
Get env named by the key and compare it with comparedEnv.
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
}
CompareOsEnv
Execute shell command, return the stdout and stderr string of command, and error if error occur. param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1. In linux, use /bin/bash -c to execute command, In windows, use powershell.exe to execute command.
Signature:
func ExecCommand(command string) (stdout, stderr string, err error)
Example:
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
Get current os bits, 32bit or 64bit. return 32 or 64
Signature:
func GetOsBits() int
Example:
import (
"fmt"
"github.com/duke-git/lancet/v2/system"
)
func main() {
osBit := system.GetOsBits()
fmt.Println(osBit)
}