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]
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

你可能感兴趣的:(golang,golang)