Go语言常用库
文本主要介绍Go常用的一些系统库:
sort、math、copy、strconv、crypto
1、sort
package mainimport ("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 mainimport ("fmt""math"
)func main() {// int32 最大最小值// 实际值:1<<31-1// 2147483647fmt.Println(math.MaxInt32)// 实际值:-1<<31// -2147483648fmt.Println(math.MinInt32)// int64 最大最小值(int默认是int64)// 9223372036854775807fmt.Println(math.MaxInt64)// -9223372036854775808fmt.Println(math.MinInt64)
}
3、copy
package mainimport "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) intcopy(a[i:], a[i+1:])a = a[:len(a)-1]// [0 1 3 4 5 6]fmt.Println(a)// make创建长度,则通过索引赋值n := 10b := 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 mainimport ("fmt""strconv"
)func main() {// byte转数字s := "12345"// s[0]类型是byte// 1num := int(s[0] - '0')// "1"str := string(s[0])// '1'b := byte(num + '0')// 111fmt.Printf("%d%s%c\n", num, str, b)// 字符串转数字num1, _ := strconv.Atoi("123")// 123fmt.Println(num1)// 数字转字符串str1 := strconv.Itoa(123)// 123fmt.Println(str1)
}
5、crypto
Go 中使用 AES 对称加密来加密和解密数据。
package mainimport ("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