Documentation
¶
Overview ¶
Package oss 操作系统相关.
Index ¶
- Constants
- Variables
- func Abs(path string) (string, error)
- func AppendFile(filename string) (*os.File, error)
- func Cancel(cancel context.CancelFunc)
- func CancelClose(closers ...io.Closer)
- func CancelContext(ctx context.Context) (context.Context, context.CancelFunc)
- func CancelFunc(cancel context.CancelFunc) func() error
- func Exist(path string) bool
- func IsDarwin() bool
- func IsDir(path string) bool
- func IsLinux() bool
- func IsRelease() bool
- func IsWindows() bool
- func Open(file string) error
- func Remove(path string, depth int) error
- func Show(file string) error
- type ProcInfo
Examples ¶
Constants ¶
View Source
const ( DefaultFileMode os.FileMode = 0o664 DefaultDirFileMod os.FileMode = 0o771 )
Variables ¶
View Source
var ( // Version 版本号. Version = "" // BuildTime 编译时间. BuildTime = "" )
nolint: gochecknoglobals
View Source
var ErrNotDefined = errors.New("OS is not defined")
ErrNotDefined 系统不支持.
Functions ¶
func Abs ¶
Abs 返回路径的绝对表示,~符号表示用户根目录.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/xuender/kit/los"
"github.com/xuender/kit/oss"
)
func main() {
home, _ := os.UserHomeDir()
fmt.Println(los.Must(oss.Abs("~/file")) == filepath.Join(home, "file"))
}
Output: true
func AppendFile ¶ added in v1.0.10
AppendFile appends the given file.
Example ¶
ExampleAppendFile is an example function.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/xuender/kit/oss"
)
func main() {
file, err := oss.AppendFile(filepath.Join(os.TempDir(), "go-cli", "create.txt"))
fmt.Println(err)
fmt.Println(file.WriteString("123"))
file.Close()
file, err = oss.AppendFile(filepath.Join(os.TempDir(), "go-cli", "create.txt"))
fmt.Println(err)
_, _ = file.WriteString("aaaa")
file.Close()
data, _ := os.ReadFile(file.Name())
fmt.Println(string(data))
os.Remove(file.Name())
}
Output: <nil> 3 <nil> <nil> 123aaaa
func Cancel ¶ added in v1.0.21
func Cancel(cancel context.CancelFunc)
Example ¶
package main
import (
"fmt"
"os"
"syscall"
"time"
"github.com/xuender/kit/oss"
)
func main() {
cancel := func() {
fmt.Println("cancel")
}
go oss.Cancel(cancel)
time.Sleep(time.Millisecond * 5)
_ = syscall.Kill(os.Getpid(), syscall.SIGINT)
time.Sleep(time.Millisecond * 5)
}
Output: cancel
func CancelClose ¶ added in v1.0.51
func CancelContext ¶ added in v1.0.21
Example ¶
package main
import (
"context"
"fmt"
"os"
"syscall"
"time"
"github.com/xuender/kit/oss"
)
func main() {
ctx, _ := oss.CancelContext(context.Background())
go func() {
<-ctx.Done()
fmt.Println("cancel")
}()
time.Sleep(time.Millisecond * 5)
_ = syscall.Kill(os.Getpid(), syscall.SIGINT)
time.Sleep(time.Millisecond * 5)
}
Output: cancel
func CancelFunc ¶ added in v1.0.21
func CancelFunc(cancel context.CancelFunc) func() error
func IsDarwin ¶
func IsDarwin() bool
IsDarwin 是否是 Darwin 系统.
Example ¶
package main
import (
"fmt"
"github.com/xuender/kit/oss"
)
func main() {
fmt.Println(oss.IsDarwin())
}
Output: false
func IsLinux ¶
func IsLinux() bool
IsLinux 是否是 Linux 系统.
Example ¶
package main
import (
"fmt"
"github.com/xuender/kit/oss"
)
func main() {
fmt.Println(oss.IsLinux())
}
Output: true
func IsRelease ¶ added in v1.0.19
func IsRelease() bool
IsRelease 是否是发布模式.
Example ¶
ExampleIsRelease 是否是发布模式例子.
package main
import (
"fmt"
"github.com/xuender/kit/oss"
)
func main() {
fmt.Println(oss.IsRelease())
oss.Version = "1.0.1"
fmt.Println(oss.IsRelease())
}
Output: false true
Types ¶
type ProcInfo ¶ added in v1.0.29
type ProcInfo struct {
Name string `json:"name"`
Dir string `json:"dir"`
Pid int `json:"pid"`
Version string `json:"version"`
BuildTime string `json:"buildTime"`
StartTime time.Time `json:"startTime"`
RunTime time.Duration `json:"runTime"`
}
func NewProcInfo ¶ added in v1.0.30
func NewProcInfo() *ProcInfo
Click to show internal directories.
Click to hide internal directories.