golang操作mysql
1. 准备表
创建数据库
1create database go_db DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
创建表
12345CREATE TABLE user_tb1( id INTEGER PRIMARY KEY AUTO_INCREMENT, username VARCHAR(20), `password` VARCHAR(20))
添加数据
12INSERT INTO user_tb1(username,password) VALUES("tom", "123");INSERT INTO user_tb1(username,password) VALUES("kite", "456");
2. 安装驱动
1go get -u github.com/go-sql-driver/mysql
初始化模块
1go mod init m
更新依赖
1go mod tidy
3. 获取连接
123456789101112131 ...
golang标准库math
该包包含一些常量和一些有用的数学计算函数,例如:三角函数、随机数、绝对值、平方根等
数学常量
1234567math.E //自然对数的底,2.718281828459045math.Pi //圆周率,3.141592653589793math.Phi //黄金分割,长/短,1.618033988749895math.MaxInt //9223372036854775807uint64(math.MaxUint) //得先把MaxUint转成uint64才能输出,18446744073709551615math.MaxFloat64 //1.7976931348623157e+308math.SmallestNonzeroFloat64 //最小的非0且正的浮点数,5e-324
NaN(Not a Number)
12f := math.NaN()math.IsNaN(f)
常用函数
12345678910math.Ceil(1.1) //向上取整,2math.Floor(1.9) //向下取整,1。 math.Floor(-1.9)=-2math.Trunc(1.9) //取整数部分 ...
golang标准库xml
xml包实现xml解析
1. 核心的两个函数
func Marsha1(v interface{}) ([]byte, error)
将struct编码成xml,可以接收任意类型
func Unmarshal(data []byte, v interface{}) error
将xml转码成struct
2. 两个核心结构体
123type Decoder struct { ...}
从输入流读取并解析xml
123type Encoder struct { ...}
写xml到输出流
3. 实例
3.1 结构体转xml
12345678910111213141516171819202122232425262728package mainimport ( "encoding/xml" "fmt" "log")type Person struct { XMLName xml.Name `xml:"persion&quo ...
golang标准库json
这个包可以实现json编码和解码,就是将json字符串转换为struct,或者将struct转换为json
1. 核心的两个函数
func Marshl(v interface{}) ([]byte, error)
将struct编码成json,可以接收任意类型
func Unmarshl(data []byte, v interface{}) error
将json转码成struct
2. 两个核心结构体
123type Decoder struct { // contains filtered or unexported fields}
从输入流取并解析json
123type Encoder struct { // contains filtered or unexported fields}
写json到输入流
3. 实例
3.1 结构体转json
123456789101112131415161718192021222324252627package mainimport ( ...
golang标准库time
time包提供测量和显示时间的功能
1. 基本使用
打印显示出现在的时间
其中now为time.Time类型,Month为time.Month类型
12345678910111213141516171819package mainimport ( "fmt" "time")func main() { now := time.Now() // 获取当前时间 fmt.Printf("now: %v\n", now) year := now.Year() month := now.Month() day := now.Day() hour := now.Hour() minute := now.Minute() second := now.Second() fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)}
运行结果:
now: 2022-12-23 22:18:19.6534 ...
golang标准库sort包
sort包提供了排序切片和用户自动逸数据集以及相关功能的函数
sort包主要针对[]int、[]float64、[]string、以及其他自定义切片的排序
1. 结构体
123type IntSlice structtype Float64Slicetype StringSlice
2. 函数
1234567891011121314151617func Ints(a []int)func IntsAreSorted(a []int) boolfunc SearchInts(a []int, x int) intfunc Float64s(a []float64)func Float64sAreSorted(a []float64) boolfunc SearchFloat64s(a []float64, x float64) intfunc Strings(a []string)func StringsAreSorted(a []string) boolfunc SearchStrings(a []string, x string) intfunc Sort(data Interfac ...
golang标准库errors
errors包实现了操作错误的函数。语言使用error类型来返回函数执行过程中遇到的错误,如果返回的error值为nil,则表示未遇到错误,否则error会返回一个字符串。用于说明遇到了什么错误。
1. error结构
123type error interface { Error() string}
你可以用任何类型去实现它(只要添加一个Error()方法即可),也就是说,error可以是任何类型,这意味着,函数返回的error值实际可以包含任意信息,不一定是字符串。
error不一定表示一个错误,它可以表示任何信息,比如io包中就用error类型的io.EOF表示数据读取结束,而不是遇到了什么错误。
errors包实现了一个最简单的error类型,只包含一个字符串,它可以记录大多数情况下遇到的错误信息,errors包的用法也很简单,只有一个New函数,用于生产一个最简单的error对象
func New(text string) error
123456789101112131415161718192021222324package mainimport ...
golang标准库bytes
bytes包提供了对字节切片进行读写操作的一系列函数,字节切片处理的函数比较多分为基本处理函数、比较函数、后缀检查函数、索引函数、分割函数、大小写处理函数和子切片处理函数等。
1. 常用函数
bytes.Contains
123456789101112131415package mainimport ( "bytes" "fmt")func main() { b := []byte("golang.google.cn") sublice1 := []byte("golang.google") sublice2 := []byte("Golang.google") fmt.Println(bytes.Contains(b, sublice1)) fmt.Println(bytes.Contains(b, sublice2))}
运行结果:
true
false
bytes.Count
123456789101112131415161718package mainimpo ...
golang标准库builtion
这个包提供了一些类型声明、变量和常量声明,还有一些便利函数,这个包不需要导入,这些变量和函数就可以直接使用。
1. 常用函数
1.1 append
1234func append(slice []type, elems ...Type) []Typeslice = append(slice, elem1, elem2) // 直接在slice后面添加单个元素,添加元素类型可以和slice相同,可以不同slice = append(slice, anotherSlice...) //直接将另外一个slice添加到slice后面,但其本质还是将anotherSlice中的元素一个一个添加到slice中。
实例
1234567891011121314package mainimport "fmt"func main() { i := []int{1, 3, 4, 5} i = append(i, 4) fmt.Printf("i: %v\n", i) i2 := []int{7, 9, 9} i2 = ...
golang标准库log
1. 简介
golang内置了log包,实现简单的日志服务。通过调用log包的函数,可以实现简单的日志打印功能。
2. log使用
log包中有3个系列的日志打印函数,分别print系列、panic系列、fatal系列
函数系列
作用
print
单纯打印日志
panic
打印日志,抛出panic异常
fatal
打印日志,强制结束程序(os.Exit(1)),defer函数不会执行
3. 实例
123456789101112131415161718package mainimport ( "fmt" "log")func main() { defer fmt.Println("发生了 panic错误。") log.Print("my log") log.Printf("my log %d\n", 100) name := "tom" age := 20 log.Println(name, ",", age) ...