mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
doc: normalize document
This commit is contained in:
724
docs/strutil.md
724
docs/strutil.md
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
203
docs/system.md
203
docs/system.md
@@ -1,16 +1,17 @@
|
|||||||
# System
|
# System
|
||||||
|
|
||||||
Package system contains some functions about os, runtime, shell command.
|
Package system contains some functions about os, runtime, shell command.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/system/os.go](https://github.com/duke-git/lancet/blob/main/system/os.go)
|
- [https://github.com/duke-git/lancet/blob/main/system/os.go](https://github.com/duke-git/lancet/blob/main/system/os.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
@@ -20,23 +21,23 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
- [IsWindows](#IsWindows)
|
|
||||||
- [IsLinux](#IsLinux)
|
|
||||||
- [IsMac](#IsMac)
|
|
||||||
- [GetOsEnv](#GetOsEnv)
|
|
||||||
- [SetOsEnv](#SetOsEnv)
|
|
||||||
- [RemoveOsEnv](#RemoveOsEnv)
|
|
||||||
- [CompareOsEnv](#CompareOsEnv)
|
|
||||||
- [ExecCommand](#ExecCommand)
|
|
||||||
- [GetOsBits](#GetOsBits)
|
|
||||||
|
|
||||||
|
- [IsWindows](#IsWindows)
|
||||||
|
- [IsLinux](#IsLinux)
|
||||||
|
- [IsMac](#IsMac)
|
||||||
|
- [GetOsEnv](#GetOsEnv)
|
||||||
|
- [SetOsEnv](#SetOsEnv)
|
||||||
|
- [RemoveOsEnv](#RemoveOsEnv)
|
||||||
|
- [CompareOsEnv](#CompareOsEnv)
|
||||||
|
- [ExecCommand](#ExecCommand)
|
||||||
|
- [GetOsBits](#GetOsBits)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsWindows">IsWindows</span>
|
### <span id="IsWindows">IsWindows</span>
|
||||||
|
|
||||||
<p>Check if current os is windows.</p>
|
<p>Check if current os is windows.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -44,24 +45,23 @@ import (
|
|||||||
```go
|
```go
|
||||||
func IsWindows() bool
|
func IsWindows() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
isOsWindows := system.IsWindows()
|
isOsWindows := system.IsWindows()
|
||||||
fmt.Println(isOsWindows)
|
fmt.Println(isOsWindows)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsLinux">IsLinux</span>
|
### <span id="IsLinux">IsLinux</span>
|
||||||
|
|
||||||
<p>Check if current os is linux.</p>
|
<p>Check if current os is linux.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -69,23 +69,23 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsLinux() bool
|
func IsLinux() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
isOsLinux := system.IsLinux()
|
isOsLinux := system.IsLinux()
|
||||||
fmt.Println(isOsLinux)
|
fmt.Println(isOsLinux)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsMac">IsMac</span>
|
### <span id="IsMac">IsMac</span>
|
||||||
|
|
||||||
<p>Check if current os is macos.</p>
|
<p>Check if current os is macos.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -93,23 +93,23 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsMac() bool
|
func IsMac() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
isOsMac := system.IsMac()
|
isOsMac := system.IsMac()
|
||||||
fmt.Println(isOsMac)
|
fmt.Println(isOsMac)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetOsEnv">GetOsEnv</span>
|
### <span id="GetOsEnv">GetOsEnv</span>
|
||||||
|
|
||||||
<p>Gets the value of the environment variable named by the key.</p>
|
<p>Gets the value of the environment variable named by the key.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -117,23 +117,29 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetOsEnv(key string) string
|
func GetOsEnv(key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fooEnv := system.GetOsEnv("foo")
|
err := system.SetOsEnv("foo", "abc")
|
||||||
fmt.Println(fooEnv)
|
result := system.GetOsEnv("foo")
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
fmt.Println(result)
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
// abc
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SetOsEnv">SetOsEnv</span>
|
### <span id="SetOsEnv">SetOsEnv</span>
|
||||||
|
|
||||||
<p>Sets the value of the environment variable named by the key.</p>
|
<p>Sets the value of the environment variable named by the key.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -141,24 +147,29 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SetOsEnv(key, value string) error
|
func SetOsEnv(key, value string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := system.SetOsEnv("foo", "foo_value")
|
err := system.SetOsEnv("foo", "abc")
|
||||||
fmt.Println(err)
|
result := system.GetOsEnv("foo")
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
fmt.Println(result)
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
// abc
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
||||||
|
|
||||||
<p>Remove a single environment variable.</p>
|
<p>Remove a single environment variable.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -166,25 +177,37 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RemoveOsEnv(key string) error
|
func RemoveOsEnv(key string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := system.RemoveOsEnv("foo")
|
err1 := system.SetOsEnv("foo", "abc")
|
||||||
if err != nil {
|
result1 := GetOsEnv("foo")
|
||||||
fmt.Println(err)
|
|
||||||
}
|
err2 := system.RemoveOsEnv("foo")
|
||||||
|
result2 := GetOsEnv("foo")
|
||||||
|
|
||||||
|
fmt.Println(err1)
|
||||||
|
fmt.Println(err2)
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
// <nil>
|
||||||
|
// abc
|
||||||
|
//
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CompareOsEnv">CompareOsEnv</span>
|
### <span id="CompareOsEnv">CompareOsEnv</span>
|
||||||
|
|
||||||
<p>Get env named by the key and compare it with comparedEnv.</p>
|
<p>Get env named by the key and compare it with comparedEnv.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -192,25 +215,32 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CompareOsEnv(key, comparedEnv string) bool
|
func CompareOsEnv(key, comparedEnv string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
system.SetOsEnv("foo", "foo_value")
|
err := system.SetOsEnv("foo", "abc")
|
||||||
res := system.CompareOsEnv("foo", "foo_value")
|
if err != nil {
|
||||||
fmt.Println(res) //true
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result := system.CompareOsEnv("foo", "abc")
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ExecCommand">CompareOsEnv</span>
|
### <span id="ExecCommand">CompareOsEnv</span>
|
||||||
|
|
||||||
<p>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.</p>
|
<p>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.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -218,40 +248,39 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ExecCommand(command string) (stdout, stderr string, err error)
|
func ExecCommand(command string) (stdout, stderr string, err error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// linux or mac
|
// linux or mac
|
||||||
stdout, stderr, err := system.ExecCommand("ls")
|
stdout, stderr, err := system.ExecCommand("ls")
|
||||||
fmt.Println("std out: ", stdout)
|
fmt.Println("std out: ", stdout)
|
||||||
fmt.Println("std err: ", stderr)
|
fmt.Println("std err: ", stderr)
|
||||||
assert.Equal("", stderr)
|
assert.Equal("", stderr)
|
||||||
|
|
||||||
// windows
|
// windows
|
||||||
stdout, stderr, err = system.ExecCommand("dir")
|
stdout, stderr, err = system.ExecCommand("dir")
|
||||||
fmt.Println("std out: ", stdout)
|
fmt.Println("std out: ", stdout)
|
||||||
fmt.Println("std err: ", stderr)
|
fmt.Println("std err: ", stderr)
|
||||||
|
|
||||||
// error command
|
// error command
|
||||||
stdout, stderr, err = system.ExecCommand("abc")
|
stdout, stderr, err = system.ExecCommand("abc")
|
||||||
fmt.Println("std out: ", stdout)
|
fmt.Println("std out: ", stdout)
|
||||||
fmt.Println("std err: ", stderr)
|
fmt.Println("std err: ", stderr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetOsBits">GetOsBits</span>
|
### <span id="GetOsBits">GetOsBits</span>
|
||||||
|
|
||||||
<p>Get current os bits, 32bit or 64bit. return 32 or 64</p>
|
<p>Get current os bits, 32bit or 64bit. return 32 or 64</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -259,19 +288,17 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetOsBits() int
|
func GetOsBits() int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
osBit := system.GetOsBits()
|
osBit := system.GetOsBits()
|
||||||
fmt.Println(osBit)
|
fmt.Println(osBit) // 32 or 64
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
# System
|
# System
|
||||||
system包含os, runtime, shell command相关函数。
|
|
||||||
|
system 包含 os, runtime, shell command 相关函数。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/system/os.go](https://github.com/duke-git/lancet/blob/main/system/os.go)
|
- [https://github.com/duke-git/lancet/blob/main/system/os.go](https://github.com/duke-git/lancet/blob/main/system/os.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
@@ -20,23 +21,23 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
- [IsWindows](#IsWindows)
|
|
||||||
- [IsLinux](#IsLinux)
|
|
||||||
- [IsMac](#IsMac)
|
|
||||||
- [GetOsEnv](#GetOsEnv)
|
|
||||||
- [SetOsEnv](#SetOsEnv)
|
|
||||||
- [RemoveOsEnv](#RemoveOsEnv)
|
|
||||||
- [CompareOsEnv](#CompareOsEnv)
|
|
||||||
- [ExecCommand](#ExecCommand)
|
|
||||||
- [GetOsBits](#GetOsBits)
|
|
||||||
|
|
||||||
|
- [IsWindows](#IsWindows)
|
||||||
|
- [IsLinux](#IsLinux)
|
||||||
|
- [IsMac](#IsMac)
|
||||||
|
- [GetOsEnv](#GetOsEnv)
|
||||||
|
- [SetOsEnv](#SetOsEnv)
|
||||||
|
- [RemoveOsEnv](#RemoveOsEnv)
|
||||||
|
- [CompareOsEnv](#CompareOsEnv)
|
||||||
|
- [ExecCommand](#ExecCommand)
|
||||||
|
- [GetOsBits](#GetOsBits)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation文档
|
## Documentation 文档
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsWindows">IsWindows</span>
|
### <span id="IsWindows">IsWindows</span>
|
||||||
|
|
||||||
<p>检查当前操作系统是否是windows</p>
|
<p>检查当前操作系统是否是windows</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -44,24 +45,23 @@ import (
|
|||||||
```go
|
```go
|
||||||
func IsWindows() bool
|
func IsWindows() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
isOsWindows := system.IsWindows()
|
isOsWindows := system.IsWindows()
|
||||||
fmt.Println(isOsWindows)
|
fmt.Println(isOsWindows)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsLinux">IsLinux</span>
|
### <span id="IsLinux">IsLinux</span>
|
||||||
|
|
||||||
<p>检查当前操作系统是否是linux</p>
|
<p>检查当前操作系统是否是linux</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -69,23 +69,23 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsLinux() bool
|
func IsLinux() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
isOsLinux := system.IsLinux()
|
isOsLinux := system.IsLinux()
|
||||||
fmt.Println(isOsLinux)
|
fmt.Println(isOsLinux)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsMac">IsMac</span>
|
### <span id="IsMac">IsMac</span>
|
||||||
|
|
||||||
<p>检查当前操作系统是否是macos</p>
|
<p>检查当前操作系统是否是macos</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -93,23 +93,23 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsMac() bool
|
func IsMac() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
isOsMac := system.IsMac()
|
isOsMac := system.IsMac()
|
||||||
fmt.Println(isOsMac)
|
fmt.Println(isOsMac)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetOsEnv">GetOsEnv</span>
|
### <span id="GetOsEnv">GetOsEnv</span>
|
||||||
|
|
||||||
<p>获取key命名的环境变量的值</p>
|
<p>获取key命名的环境变量的值</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -117,23 +117,29 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetOsEnv(key string) string
|
func GetOsEnv(key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fooEnv := system.GetOsEnv("foo")
|
err := system.SetOsEnv("foo", "abc")
|
||||||
fmt.Println(fooEnv)
|
result := system.GetOsEnv("foo")
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
fmt.Println(result)
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
// abc
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SetOsEnv">SetOsEnv</span>
|
### <span id="SetOsEnv">SetOsEnv</span>
|
||||||
|
|
||||||
<p>设置由key命名的环境变量的值</p>
|
<p>设置由key命名的环境变量的值</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -141,24 +147,29 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SetOsEnv(key, value string) error
|
func SetOsEnv(key, value string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := system.SetOsEnv("foo", "foo_value")
|
err := system.SetOsEnv("foo", "abc")
|
||||||
fmt.Println(err)
|
result := system.GetOsEnv("foo")
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
fmt.Println(result)
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
// abc
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
||||||
|
|
||||||
<p>删除单个环境变量</p>
|
<p>删除单个环境变量</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -166,25 +177,37 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RemoveOsEnv(key string) error
|
func RemoveOsEnv(key string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := system.RemoveOsEnv("foo")
|
err1 := system.SetOsEnv("foo", "abc")
|
||||||
if err != nil {
|
result1 := GetOsEnv("foo")
|
||||||
fmt.Println(err)
|
|
||||||
}
|
err2 := system.RemoveOsEnv("foo")
|
||||||
|
result2 := GetOsEnv("foo")
|
||||||
|
|
||||||
|
fmt.Println(err1)
|
||||||
|
fmt.Println(err2)
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
// <nil>
|
||||||
|
// abc
|
||||||
|
//
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CompareOsEnv">CompareOsEnv</span>
|
### <span id="CompareOsEnv">CompareOsEnv</span>
|
||||||
|
|
||||||
<p>获取key命名的环境变量值并与compareEnv进行比较</p>
|
<p>获取key命名的环境变量值并与compareEnv进行比较</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -192,25 +215,32 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CompareOsEnv(key, comparedEnv string) bool
|
func CompareOsEnv(key, comparedEnv string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
system.SetOsEnv("foo", "foo_value")
|
err := system.SetOsEnv("foo", "abc")
|
||||||
res := system.CompareOsEnv("foo", "foo_value")
|
if err != nil {
|
||||||
fmt.Println(res) //true
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result := system.CompareOsEnv("foo", "abc")
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ExecCommand">ExecCommand</span>
|
### <span id="ExecCommand">ExecCommand</span>
|
||||||
|
|
||||||
<p>执行shell命令,返回命令的stdout和stderr字符串,如果出现错误,则返回错误。参数`command`是一个完整的命令字符串,如ls-a(linux),dir(windows),ping 127.0.0.1。在linux中,使用/bin/bash-c执行命令,在windows中,使用powershell.exe执行命令。</p>
|
<p>执行shell命令,返回命令的stdout和stderr字符串,如果出现错误,则返回错误。参数`command`是一个完整的命令字符串,如ls-a(linux),dir(windows),ping 127.0.0.1。在linux中,使用/bin/bash-c执行命令,在windows中,使用powershell.exe执行命令。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -218,40 +248,39 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ExecCommand(command string) (stdout, stderr string, err error)
|
func ExecCommand(command string) (stdout, stderr string, err error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// linux or mac
|
// linux or mac
|
||||||
stdout, stderr, err := system.ExecCommand("ls")
|
stdout, stderr, err := system.ExecCommand("ls")
|
||||||
fmt.Println("std out: ", stdout)
|
fmt.Println("std out: ", stdout)
|
||||||
fmt.Println("std err: ", stderr)
|
fmt.Println("std err: ", stderr)
|
||||||
assert.Equal("", stderr)
|
assert.Equal("", stderr)
|
||||||
|
|
||||||
// windows
|
// windows
|
||||||
stdout, stderr, err = system.ExecCommand("dir")
|
stdout, stderr, err = system.ExecCommand("dir")
|
||||||
fmt.Println("std out: ", stdout)
|
fmt.Println("std out: ", stdout)
|
||||||
fmt.Println("std err: ", stderr)
|
fmt.Println("std err: ", stderr)
|
||||||
|
|
||||||
// error command
|
// error command
|
||||||
stdout, stderr, err = system.ExecCommand("abc")
|
stdout, stderr, err = system.ExecCommand("abc")
|
||||||
fmt.Println("std out: ", stdout)
|
fmt.Println("std out: ", stdout)
|
||||||
fmt.Println("std err: ", stderr)
|
fmt.Println("std err: ", stderr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetOsBits">GetOsBits</span>
|
### <span id="GetOsBits">GetOsBits</span>
|
||||||
|
|
||||||
<p>获取当前操作系统位数,返回32或64</p>
|
<p>获取当前操作系统位数,返回32或64</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -259,23 +288,17 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetOsBits() int
|
func GetOsBits() int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/system"
|
"github.com/duke-git/lancet/v2/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
osBit := system.GetOsBits()
|
osBit := system.GetOsBits()
|
||||||
fmt.Println(osBit)
|
fmt.Println(osBit) // 32 or 64
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
# Xerror
|
# Xerror
|
||||||
|
|
||||||
Package xerror implements helpers for errors.
|
Package xerror implements helpers for errors.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/xerror/xerror.go](https://github.com/duke-git/lancet/blob/main/xerror/xerror.go)
|
- [https://github.com/duke-git/lancet/blob/main/xerror/xerror.go](https://github.com/duke-git/lancet/blob/main/xerror/xerror.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/xerror"
|
"github.com/duke-git/lancet/v2/xerror"
|
||||||
@@ -19,15 +21,15 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
- [Unwrap](#Unwrap)
|
|
||||||
|
- [Unwrap](#Unwrap)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unwrap">Unwrap</span>
|
### <span id="Unwrap">Unwrap</span>
|
||||||
|
|
||||||
<p>Unwrap if err is nil then it returns a valid value. If err is not nil, Unwrap panics with err.</p>
|
<p>Unwrap if err is nil then it returns a valid value. If err is not nil, Unwrap panics with err.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -35,6 +37,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Unwrap[T any](val T, err error) T
|
func Unwrap[T any](val T, err error) T
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -46,20 +49,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := xerror.Unwrap(strconv.Atoi("42"))
|
result1 := xerror.Unwrap(strconv.Atoi("42"))
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
|
|
||||||
_, err := strconv.Atoi("4o2")
|
_, err := strconv.Atoi("4o2")
|
||||||
defer func() {
|
defer func() {
|
||||||
v := recover()
|
v := recover()
|
||||||
result2 := reflect.DeepEqual(err.Error(), v.(*strconv.NumError).Error())
|
result2 := reflect.DeepEqual(err.Error(), v.(*strconv.NumError).Error())
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
xerror.Unwrap(strconv.Atoi("4o2"))
|
xerror.Unwrap(strconv.Atoi("4o2"))
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 42
|
// 42
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
# Xerror
|
# Xerror
|
||||||
xerror错误处理逻辑封装
|
|
||||||
|
xerror 错误处理逻辑封装
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/xerror/xerror.go](https://github.com/duke-git/lancet/blob/main/xerror/xerror.go)
|
- [https://github.com/duke-git/lancet/blob/main/xerror/xerror.go](https://github.com/duke-git/lancet/blob/main/xerror/xerror.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/xerror"
|
"github.com/duke-git/lancet/v2/xerror"
|
||||||
@@ -19,15 +21,15 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
- [Unwrap](#Unwrap)
|
|
||||||
|
- [Unwrap](#Unwrap)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unwrap">Unwrap</span>
|
### <span id="Unwrap">Unwrap</span>
|
||||||
|
|
||||||
<p>检查error, 如果err为nil则展开,则它返回一个有效值,如果err不是nil则Unwrap使用err发生panic。</p>
|
<p>检查error, 如果err为nil则展开,则它返回一个有效值,如果err不是nil则Unwrap使用err发生panic。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -35,6 +37,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Unwrap[T any](val T, err error) T
|
func Unwrap[T any](val T, err error) T
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -46,20 +49,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := xerror.Unwrap(strconv.Atoi("42"))
|
result1 := xerror.Unwrap(strconv.Atoi("42"))
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
|
|
||||||
_, err := strconv.Atoi("4o2")
|
_, err := strconv.Atoi("4o2")
|
||||||
defer func() {
|
defer func() {
|
||||||
v := recover()
|
v := recover()
|
||||||
result2 := reflect.DeepEqual(err.Error(), v.(*strconv.NumError).Error())
|
result2 := reflect.DeepEqual(err.Error(), v.(*strconv.NumError).Error())
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
xerror.Unwrap(strconv.Atoi("4o2"))
|
xerror.Unwrap(strconv.Atoi("4o2"))
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 42
|
// 42
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ package system
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func ExampleSetOsEnv() {
|
func ExampleSetOsEnv() {
|
||||||
ok := SetOsEnv("foo", "abc")
|
err := SetOsEnv("foo", "abc")
|
||||||
result := GetOsEnv("foo")
|
result := GetOsEnv("foo")
|
||||||
|
|
||||||
fmt.Println(ok)
|
fmt.Println(err)
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
// Output:
|
// Output:
|
||||||
// <nil>
|
// <nil>
|
||||||
@@ -25,14 +25,14 @@ func ExampleGetOsEnv() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ExampleRemoveOsEnv() {
|
func ExampleRemoveOsEnv() {
|
||||||
ok1 := SetOsEnv("foo", "abc")
|
err1 := SetOsEnv("foo", "abc")
|
||||||
result1 := GetOsEnv("foo")
|
result1 := GetOsEnv("foo")
|
||||||
|
|
||||||
ok2 := RemoveOsEnv("foo")
|
err2 := RemoveOsEnv("foo")
|
||||||
result2 := GetOsEnv("foo")
|
result2 := GetOsEnv("foo")
|
||||||
|
|
||||||
fmt.Println(ok1)
|
fmt.Println(err1)
|
||||||
fmt.Println(ok2)
|
fmt.Println(err2)
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|
||||||
@@ -49,9 +49,10 @@ func ExampleCompareOsEnv() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result1 := CompareOsEnv("foo", "abc")
|
result := CompareOsEnv("foo", "abc")
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
fmt.Println(result1)
|
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user