oss

package
v1.0.39 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2023 License: MIT Imports: 14 Imported by: 20

README

oss

操作系统相关函数。

goos

IsDarwin

是否是 Darwin 系统

IsLinux

是否是 Linux 系统

IsWindows

是否是 Windows 系统

os

Open

打开文件

Show

在目录中显示

path

Abs

返回路径的绝对表示,~符号表示用户根目录。

Exist

文件或目录是否存在。

IsDir

路径是否是目录。

Documentation

Overview

Package oss 操作系统相关.

Index

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

func Abs(path string) (string, error)

Abs 返回路径的绝对表示,~符号表示用户根目录.

Example
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/samber/lo"
	"github.com/xuender/kit/oss"
)

func main() {
	home, _ := os.UserHomeDir()
	fmt.Println(lo.Must1(oss.Abs("~/file")) == filepath.Join(home, "file"))

}
Output:

true

func AppendFile added in v1.0.10

func AppendFile(filename string) (*os.File, error)

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 CancelContext added in v1.0.21

func CancelContext(ctx context.Context) (context.Context, context.CancelFunc)
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 Exist

func Exist(path string) bool

Exist 文件或目录是否存在.

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 IsDir

func IsDir(path string) bool

IsDir 路径是否是目录.

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

func IsWindows

func IsWindows() bool

IsWindows 是否是 Windows 系统.

Example
package main

import (
	"fmt"

	"github.com/xuender/kit/oss"
)

func main() {
	fmt.Println(oss.IsWindows())

}
Output:

false

func Open

func Open(file string) error

Open 打开文件.

func Remove added in v1.0.14

func Remove(path string, depth int) error

Remove 文件或目录,如果父目录为空则删除.

func Show

func Show(file string) error

Show 在目录中显示.

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

func (*ProcInfo) String added in v1.0.29

func (p *ProcInfo) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL