gf

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2023 License: Apache-2.0 Imports: 8 Imported by: 4

README

gf

介绍

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

功能清单

  • 文本增删改查
  • 文件/文件夹增删改
  • 文本单行单列范围行范围列截取

截取中首/都是1开始 截取中首/都是1开始

安装

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.NewFile("./text/dir")
	f.Mkdir("./text/dir", 0666)
	if f.Err != nil {
		logger.Error("创建失败")
	} else {
		logger.Info("创建成功")
	}
}

func WriteTxtAppend() {
	f := gf.NewFile("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()
}

文本字符串管理Demo1
package main

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

func main() {
	fmt.Println("坐公交也用券")

	logger.Debug("通过读取文件获取内容进行截取")
	// 对应Shell: sed -n 2p text/col.txt | awk '{print $2}'
	txt := gf.ReadFile("txt/gf.txt").GetText().Line(1).Column(2, " ").Text
	logger.Info("获取内容: ", txt)

	logger.Debug("通过赋值进行截取")
	t := "第一行 第二列\n第二行 第二列 第三列"
	st := gf.ReadFile("demo")             // 传入一个不存在的文件名称
	st.Text = t                           // 直接赋值字符串
	get := st.Line(1).Column(1, " ").Text // 对行列进行截取
	logger.Info("截取字符串内容: ", get)
}

效果

坐公交也用券
[2023-01-18 15:12:52] [DEBG] [gf.go:12] 通过读取文件获取内容进行截取
[2023-01-18 15:12:52] [INFO] [gf.go:15] 获取内容:  第二列

[2023-01-18 15:12:52] [DEBG] [gf.go:17] 通过赋值进行截取
[2023-01-18 15:12:52] [INFO] [gf.go:22] 截取字符串内容:  第二行
文本管理Demo2
package main

import (
	"testing"
	"fmt"
	"gitee.com/liumou_site/gf"
	"gitee.com/liumou_site/logger"
)
func Read() {
	f := gf.NewReadFile("./text/user.txt").GetText()
	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.NewReadFile("./text/user.txt").GetText()
	f.LineRange(1, 4)
	if f.Err != nil {
		logger.Error("读取失败: ", f.Err.Error())
	} else {
		logger.Info("读取成功")
		fmt.Println(f.Text)
	}
}
func main()  {
    Read()
	ReadLineRange()
}

效果

[2023-01-17 16:12:32] [INFO] [read_test.go:15] 所有内容读取成功
刘某
林某
梁某
第一行
第二行
第三行
第四行
第一行
第二行
第三行
第四行

[2023-01-17 16:12:33] [INFO] [read_test.go:20] 第二行内容:  林某

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Match added in v1.2.2

func Match(text, match string) (res string, err error)

Match 匹配任意关键词(任意位置)

func MatchFirst added in v1.2.2

func MatchFirst(text, match string) (res string, err error)

MatchFirst 匹配关键词(开头)

func MatchTail added in v1.2.2

func MatchTail(text, match string) (res string, err error)

MatchTail 匹配关键词(尾部)

func Version

func Version() string

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 NewFile added in v1.1.3

func NewFile(filename string) *FileApi

func (*FileApi) DeleteFile

func (fi *FileApi) DeleteFile()

DeleteFile 删除文件

func (*FileApi) EchoAdd

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

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

func (*FileApi) Exists

func (fi *FileApi) Exists()

Exists 判断路径是否存在

func (*FileApi) IsDir

func (fi *FileApi) IsDir()

IsDir 判断是否属于文件夹

func (*FileApi) IsFile

func (fi *FileApi) IsFile()

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 Handle added in v1.2.2

type Handle struct {
	Text string // 处理文本内容

	Slice []string // 处理切片内容
	Err   error    // 错误信息

	GrepErr error // 筛选错误
	ColErr  error // 列截取错误
	LineErr error // 行错误
	// contains filtered or unexported fields
}

func NewHandle added in v1.2.2

func NewHandle(text string) *Handle

func (*Handle) Column added in v1.2.2

func (rs *Handle) Column(col int, sep string) *Handle

Column 读取指定列, 首列: 1

func (*Handle) ColumnRange added in v1.2.2

func (rs *Handle) ColumnRange(start, end int, sep string) *Handle

ColumnRange 读取指定范围列, 首列: 1

func (*Handle) Line added in v1.2.2

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

Line 读取指定行,首行: 1

func (*Handle) LineRange added in v1.2.2

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

LineRange 读取指定范围行, 首行: 1

func (*Handle) Reset added in v1.2.3

func (rs *Handle) Reset(col int, sep string) *Handle

Init 重置所有数据

type ReadScreen

type ReadScreen struct {
	Text     string // 字符串内容
	Err      error  // 错误信息
	FileName string // 需要操作的文件名

	Slice []string // 切片
	// contains filtered or unexported fields
}

func NewReadFile added in v1.1.3

func NewReadFile(filename string) *ReadScreen

func (*ReadScreen) Column added in v1.1.0

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

Column 读取指定列, 首列: 1

func (*ReadScreen) ColumnRange added in v1.1.0

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

ColumnRange 读取指定范围列, 首列: 1

func (*ReadScreen) GetText added in v1.1.1

func (rs *ReadScreen) GetText() *ReadScreen

GetText 以只读方式读取文件所有内容,如果内容较多切勿使用此方法

func (*ReadScreen) Grep added in v1.2.2

func (rs *ReadScreen) Grep(match string) *ReadScreen

Grep 通过关键词筛选所有匹配行

func (*ReadScreen) Line

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

Line 读取指定行,首行: 1

func (*ReadScreen) LineRange added in v1.1.0

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

LineRange 读取指定范围行, 首行: 1

Jump to

Keyboard shortcuts

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