gf

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: Apache-2.0 Imports: 6 Imported by: 4

README

gf

介绍

gf是一个使用Go编写的文件/文本管理模块,通过管道的方式可以快速实现复杂的需求

功能清单

  • 文本匹配/筛选
  • 文件/文件夹增删改

安装

go get -u gitee.com/liumou_site/gf

使用说明

文件/文件夹管理
package main

import (
    "strings"
    "gitee.com/liumou_site/gf"
    "gitee.com/liumou_site/logger"
)

func Mkdir() {
	f := gf.File("./text/dir")
	f.Mkdir("./text/dir", 0666)
	if f.Err != nil {
		logger.Error("创建失败")
	} else {
		logger.Info("创建成功")
	}
}

func WriteTxtAppend() {
	f := gf.File("text/user.txt")
	txt := strings.Split("第一行 第二行 第三行 第四行", " ")
	for _, i := range txt {
		f.WriteTxtAppend(i, true)
		if f.Err != nil {
			logger.Error("写入失败: ", i)
		} else {
			logger.Info("写入成功: ", i)
		}
	}
}
func main()  {
    Mkdir()
    WriteTxtAppend()
}

文本管理
package main

import (
	"testing"
	"fmt"
	"gitee.com/liumou_site/gf"
	"gitee.com/liumou_site/logger"
)
func Read() {
	f := gf.ReadFile("./text/user.txt").OnlyAll()
	if f.Err != nil {
		logger.Error("读取失败: ", f.Err.Error())
	} else {
		logger.Info("所有内容读取成功")
		fmt.Println(f.Text)
	}
	f.Line(1) // 第一行是 0
	if f.Err == nil {
		logger.Info("第二行内容: ", f.Text)
	} else {
		logger.Error("获取失败: ", f.Err.Error())
	}
}

func ReadLineRange() {
	f := gf.ReadFile("./text/user.txt").OnlyAll()
	f.LineRange(1, 4)
	if f.Err != nil {
		logger.Error("读取失败: ", f.Err.Error())
	} else {
		logger.Info("读取成功")
		fmt.Println(f.Text)
	}
}
func main()  {
    Read()
	ReadLineRange()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version()

Types

type FileApi

type FileApi struct {
	Success  bool   // 是否操作成功
	AbsPath  string // 文件绝对路径
	FileName string // 需要操作的文件名
	Err      error  // 错误信息(读写操作错误的信息)
	Text     string // 最终字符串信息(例如文本读取信息)
	IsDirs   bool   // 是否属于文件夹
	IsFiles  bool   // 是否属于文件
	ExIst    bool   // 是否存在对象
	// contains filtered or unexported fields
}

func File added in v1.0.1

func File(filename string) *FileApi

func (*FileApi) DeleteFile

func (fi *FileApi) DeleteFile()

DeleteFile 删除文件

func (*FileApi) EchoAdd

func (fi *FileApi) EchoAdd(text, filename string, wrap bool) error

EchoAdd 写入文本到文件中,当文件已存在则覆盖,当文件不存在则创建,默认权限: 0666

func (*FileApi) Exists

func (fi *FileApi) Exists(path string)

Exists 判断路径是否存在

func (*FileApi) IsDir

func (fi *FileApi) IsDir(path string) (bool, error)

IsDir 判断是否属于文件夹

func (*FileApi) IsFile

func (fi *FileApi) IsFile(path string)

IsFile 判断是否属于文件

func (*FileApi) Mkdir

func (fi *FileApi) Mkdir(dir string, mode fs.FileMode)

Mkdir 递归创建文件夹(dir 文件夹路径 mode 创建模式,例如: 0777)

func (*FileApi) WriteTxtAppend

func (fi *FileApi) WriteTxtAppend(text string, wrap bool)

WriteTxtAppend 追加方式写入文本(wrap: 是否在末尾追加换行符)

type ReadScreen

type ReadScreen struct {
	Text string // 字符串内容
	Err  error  // 错误信息

	FileName string // 需要操作的文件名
	// contains filtered or unexported fields
}

func ReadFile added in v1.1.0

func ReadFile(filename string) *ReadScreen

func (*ReadScreen) Column added in v1.1.0

func (rs *ReadScreen) Column(col int, seq string) *ReadScreen

Column 读取指定列

func (*ReadScreen) ColumnRange added in v1.1.0

func (rs *ReadScreen) ColumnRange(start, end int) *ReadScreen

ColumnRange 读取指定范围列

func (*ReadScreen) Line

func (rs *ReadScreen) Line(line int) *ReadScreen

Line 读取指定行

func (*ReadScreen) LineRange added in v1.1.0

func (rs *ReadScreen) LineRange(start, end int) *ReadScreen

LineRange 读取指定范围行

func (*ReadScreen) OnlyAll added in v1.1.0

func (rs *ReadScreen) OnlyAll() *ReadScreen

OnlyAll 以只读方式读取文件所有内容

Jump to

Keyboard shortcuts

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