07_包

标准库介绍

  • unsafe: 包含了一些打破 Go 语言“类型安全”的命令,一般的程序中不会被使用,可用在 C/C++ 程序的调用中
  • syscall-os-os/exec:
    • os: 提供给我们一个平台无关性的操作系统功能接口,采用类 Unix 设计,隐藏了不同操作系统间的差异,让不同的文件系统和操作系统对象表现一致。
    • os/exec: 提供我们运行外部操作系统命令和程序的方式。
    • syscall: 底层的外部包,提供了操作系统底层调用的基本接口。
// 通过一个Go程序来重启 Linux

package main
import (
   "syscall"
)

const LINUX_REBOOT_MAGIC1 uintptr = 0xfee1dead
const LINUX_REBOOT_MAGIC2 uintptr = 672274793
const LINUX_REBOOT_CMD_RESTART uintptr = 0x1234567

func main() {
	syscall.Syscall(syscall.SYS_REBOOT,
		LINUX_REBOOT_MAGIC1,
		LINUX_REBOOT_MAGIC2,
		LINUX_REBOOT_CMD_RESTART)
}
  • archive/tar/zip-compress:压缩(解压缩)文件功能。
  • fmt-io-bufio-path/filepath-flag:
    • fmt: 提供了格式化输入输出功能。
    • io: 提供了基本输入输出功能,大多数是围绕系统功能的封装。
    • bufio: 缓冲输入输出功能的封装。
    • path/filepath: 用来操作在当前系统中的目标文件名路径。
    • flag: 对命令行参数的操作。  
  • strings-strconv-unicode-regexp-bytes:
    • strings: 提供对字符串的操作。
    • strconv: 提供将字符串转换为基础类型的功能。
    • unicode: 为 unicode 型的字符串提供特殊的功能。
    • regexp: 正则表达式功能。
    • bytes: 提供对字符型分片的操作。
    • index/suffixarray: 子字符串快速查询。
  • math-math/cmath-math/big-math/rand-sort:
    • math: 基本的数学函数。
    • math/cmath: 对复数的操作。
    • math/rand: 伪随机数生成。
    • sort: 为数组排序和自定义集合。
    • math/big: 大数的实现和计算。
  • container-/list-ring-heap: 实现对集合的操作。
    • list: 双链表。
    • ring: 环形链表。
  • time-log:
    • time: 日期和时间的基本操作。
    • log: 记录程序运行时产生的日志,我们将在后面的章节使用它。
  • encoding/json-encoding/xml-text/template:
    • encoding/json: 读取并解码和写入并编码 JSON 数据。
    • encoding/xml: 简单的 XML1.0 解析器,有关 JSON 和 XML 的实例请查阅第 12.9/10 章节。
    • text/template:生成像 HTML 一样的数据与文本混合的数据驱动模板(参见第 15.7 节)。
  • net-net/http-html:(参见第 15 章)
    • net: 网络数据的基本操作。
    • http: 提供了一个可扩展的 HTTP 服务器和基础客户端,解析 HTTP 请求和回复。
    • html: HTML5 解析器。
  • runtime: Go 程序运行时的交互操作,例如垃圾回收和协程创建。
  • reflect: 实现通过程序运行时反射,让程序操作任意类型的变量。

regexp 包

一般使用时,我们会将正则模式通过 compile 返回一个 Regexp 对象,然后进行匹配、查找、替换相关的功能。

1.1 Regexp 初始化函数

func Compile(expr string) (*Regexp, error)     //以正则表达式为基础返回Regexp实体
func MustCompile(str string) *Regexp           //如果不能解析正则表达式则直接触发panic

1.2 标准实现

func Match(pattern string, b []byte) (matched bool, err error)  //判断字节切片b是否匹配正则pattern
func MatchReader(pattern string, r io.RuneReader) (matched bool, err error)  //正则匹配 io.RuneReader
func MatchString(pattern string, s string) (matched bool, err error)   //正则匹配字符串

1.3 Regexp挂载方法

// 正则查询
func (re *Regexp) FindString(s string) string
func (re *Regexp) FindStringIndex(s string) (loc []int)
func (re *Regexp) FindAllString(s string, n int) []string
func (re *Regexp) FindAllStringIndex(s string, n int) [][]int
func (re *Regexp) FindAll(b []byte, n int) [][]byte
func (re *Regexp) FindAllIndex(b []byte, n int) [][]int
func (re *Regexp) FindIndex(b []byte) (loc []int)

// 正则匹配
func (re *Regexp) Match(pattern string, b []byte) bool
func (re *Regexp) MatchString(pattern string, s string) bool

// 正则替换
func (re *Regexp) ReplaceAll(src, repl []byte) []byte
func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte
func (re *Regexp) ReplaceAllString(src, repl string) string
func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string

测试

func TestRegexp() {
	searchIn := "John: 2578.34 William: 4567.23 Steve: 5632.18"
	pat := "[0-9]+.[0-9]+"
	// 将float32 扩大两倍
	f := func(s string) string {
		v, _ := strconv.ParseFloat(s, 32)
		return strconv.FormatFloat(v*2, 'f', 2, 32)
	}
	if ok, _ := regexp.Match(pat, []byte(searchIn)); ok {
		fmt.Println("Match Found")
	}
	re, _ := regexp.Compile(pat)
	str := re.ReplaceAllString(searchIn, "##.#")
	fmt.Println(str)

	str2 := re.ReplaceAllStringFunc(searchIn, f)
	fmt.Println(str2)
}

// 输出
Match Found
John: ##.# William: ##.# Steve: ##.#          
John: 5156.68 William: 9134.46 Steve: 11264.36

通过Git打包和安装

image-20220428230915277

  • 假设需要打包 test01/src/io 包,首先进入到 io 包下并创建一个 Git 仓库在里面:git init

  • 接着添加所有文件到仓库: git add .

  • 标记第一个版本:git commit -m "initial rivision"

  • 在云端创建一个新的 io 仓库:git remote add origin git@github.com:NNNN/uc.git

git push -u origin master

从 Github 上安装远端项目到本地机器:

  • 打开终端并执行:go get github.com/NNNN/uc
  • 这样这台机器上的其它 Go 应用程序也可以通过导入路径:"github.com/NNNN/uc" 来代替 "./uc/uc" 来使用。
posted @ 2023-10-08 13:10  Stitches  阅读(11)  评论(0)    收藏  举报