当前位置: 首页 > article >正文

Go语言常用库

Go语言常用库

文本主要介绍Go常用的一些系统库:

sort、math、copy、strconv、crypto

1、sort

package main

import (
   "fmt"
   "sort"
)

// sort
// int排序
// sort.Ints([]int{})
// 字符串排序
// sort.Strings([]string{})
// 自定义排序
// sort.Slice(s,func(i,j int)bool{return s[i]<s[j]})
func main() {
   slice1 := make([]int, 0)
   slice1 = append(slice1, 2)
   slice1 = append(slice1, 1)
   // int排序
   sort.Ints(slice1)
   // [1 2]
   fmt.Println(slice1)
   slice2 := make([]string, 0)
   slice2 = append(slice2, "2")
   slice2 = append(slice2, "1")
   // 字符串排序
   sort.Strings(slice2)
   // [1 2]
   fmt.Println(slice2)
   slice3 := make([]int, 0)
   slice3 = append(slice3, 22)
   slice3 = append(slice3, 11)
   // 自定义排序
   sort.Slice(slice3, func(i, j int) bool { return slice3[i] < slice3[j] })
   // [11 22]
   fmt.Println(slice3)
}

2、math

package main

import (
	"fmt"
	"math"
)

func main() {
	// int32 最大最小值
	// 实际值:1<<31-1
	// 2147483647
	fmt.Println(math.MaxInt32)
	// 实际值:-1<<31
	// -2147483648
	fmt.Println(math.MinInt32)
	// int64 最大最小值(int默认是int64)
	// 9223372036854775807
	fmt.Println(math.MaxInt64)
	// -9223372036854775808
	fmt.Println(math.MinInt64)
}

3、copy

package main

import "fmt"

func main() {
	a := make([]int, 0)
	a = []int{0, 1, 2, 3, 4, 5, 6}
	i := 2
	// 删除a[i],可以用copy将i+1到末尾的值覆盖到i,然后末尾-1
	// func copy(dst, src []Type) int
	copy(a[i:], a[i+1:])
	a = a[:len(a)-1]
	// [0 1 3 4 5 6]
	fmt.Println(a)
	// make创建长度,则通过索引赋值
	n := 10
	b := make([]int, n)
	b[n-1] = 100
	// [0 0 0 0 0 0 0 0 0 100]
	fmt.Println(b)
	// make长度为0,则通过append()赋值
	c := make([]int, 0)
	c = append(a, 200)
	// [0 1 3 4 5 6 200]
	fmt.Println(c)
}

4、strconv

package main

import (
	"fmt"
	"strconv"
)

func main()  {
	// byte转数字
	s := "12345"
	// s[0]类型是byte
	// 1
	num := int(s[0] - '0')
	// "1"
	str := string(s[0])
	// '1'
	b := byte(num + '0')
	// 111
	fmt.Printf("%d%s%c\n", num, str, b)
	// 字符串转数字
	num1, _ := strconv.Atoi("123")
	// 123
	fmt.Println(num1)
	// 数字转字符串
	str1 := strconv.Itoa(123)
	// 123
	fmt.Println(str1)
}

5、crypto

Go 中使用 AES 对称加密来加密和解密数据。

package main

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"io"
)

// 加密密钥,必须是 16、24 或 32 字节
var encryptionKey = []byte("12345678abcdefgh")

func encrypt(data []byte) (string, error) {
	block, err := aes.NewCipher(encryptionKey)
	if err != nil {
		return "", err
	}

	ciphertext := make([]byte, aes.BlockSize+len(data))
	iv := ciphertext[:aes.BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		return "", err
	}

	stream := cipher.NewCFBEncrypter(block, iv)
	stream.XORKeyStream(ciphertext[aes.BlockSize:], data)

	return base64.URLEncoding.EncodeToString(ciphertext), nil
}

func decrypt(encodedData string) ([]byte, error) {
	ciphertext, err := base64.URLEncoding.DecodeString(encodedData)
	if err != nil {
		return nil, err
	}

	block, err := aes.NewCipher(encryptionKey)
	if err != nil {
		return nil, err
	}

	if len(ciphertext) < aes.BlockSize {
		return nil, fmt.Errorf("加密数据长度无效")
	}

	iv := ciphertext[:aes.BlockSize]
	ciphertext = ciphertext[aes.BlockSize:]

	stream := cipher.NewCFBDecrypter(block, iv)
	stream.XORKeyStream(ciphertext, ciphertext)

	return ciphertext, nil
}

func main() {
	data := []byte("Hello")
	encryptedData, err := encrypt(data)
	if err != nil {
		fmt.Println("加密失败:", err)
		return
	}

	fmt.Println("加密后的数据:", encryptedData)

	decryptedData, err := decrypt(encryptedData)
	if err != nil {
		fmt.Println("解密失败:", err)
		return
	}

	fmt.Println("解密后的数据:", string(decryptedData))
}
加密后的数据: GaWSwBoaMaSyNdkNEnLsmapFhJIZ
解密后的数据: Hello

http://www.kler.cn/a/131519.html

相关文章:

  • 51单片机应用从零开始(六)·逻辑运算
  • 初始MySQL(七)(MySQL表类型和存储引擎,MySQL视图,MySQL用户管理)
  • final 关键字小结笔记
  • 自定义指令基础
  • 韦东山linux驱动开发学习【常更】
  • 【cfeng-work】架构演进和漫谈
  • 基于黄金正弦算法优化概率神经网络PNN的分类预测 - 附代码
  • Visual Studio Code配置c/c++环境
  • 从0开始学习数据结构 C语言实现 1.前篇及二分查找算法
  • 串口通信原理及应用
  • 基于JavaWeb+SSM+购物系统微信小程序的设计和实现
  • 系统及其存储相关
  • stable diffusion comfyui的api使用教程
  • k8s pod常用运维命令
  • JavaScrip获取视频第一帧作为封面图
  • 【EI会议征稿】第三届网络安全、人工智能与数字经济国际学术会议(CSAIDE 2024)
  • 物联网网关在工业行业的应用案例
  • Elasticsearch 应用
  • JS递归函数详解
  • 腾讯云新客户优惠服务器88元/年,540元/3年,另有5年优惠服务器