go常用代码片
Easul Lv6

日志记录

GO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package logger

import (
"fmt"
"time"
)

const (
// 黑色
_DEBUG_COLOR int = 30
// 绿色
_INFO_COLOR int = 32
// 黄色
_WARN_COLOR int = 33
// 红色
_ERROR_COLOR int = 31
)

func getCurrentTime() string {
now := time.Now()
year, month, day := now.Date()
hour, minite, second := now.Clock()
dateTime := fmt.Sprintf("%d-%d-%d %d:%d:%d", year, month, day, hour, minite, second)
return dateTime
}

// 默认传字符串消息
// 需要进行多个参数格式化时,前边为格式化字符串,后边为参数
func Debug(msg string, params ...any) {
printLog(msg, _DEBUG_COLOR, params...)
}

// 默认传字符串消息
// 需要进行多个参数格式化时,前边为格式化字符串,后边为参数
func Info(msg string, params ...any) {
printLog(msg, _INFO_COLOR, params...)
}

// 默认传字符串消息
// 需要进行多个参数格式化时,前边为格式化字符串,后边为参数
func Warn(msg string, params ...any) {
printLog(msg, _WARN_COLOR, params...)
}

// 默认传字符串消息
// 需要进行多个参数格式化时,前边为格式化字符串,后边为参数
func Error(msg string, params ...any) {
printLog(msg, _ERROR_COLOR, params...)
}

func printLog(msg string, color int, params ...any) {
if len(params) != 0 {
msg = fmt.Sprintf(msg, params...)
}
currentTime := getCurrentTime()
fmt.Printf("[%s] \033[%dm%s\033[0m\n", currentTime, color, msg)
}

数据库

sqlite

GO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)

func main() {
// 打开SQLite数据库连接
db, err := sql.Open("sqlite3", "./example.db")
if err != nil {
panic(err)
}
defer db.Close()

// 执行SQL查询
rows, err := db.Query("SELECT * FROM customers")
if err != nil {
panic(err)
}
defer rows.Close()

// 处理查询结果
for rows.Next() {
var customerID int
var name string
var email string
err := rows.Scan(&customerID, &name, &email)
if err != nil {
panic(err)
}
fmt.Printf("Customer ID: %d, Name: %s, Email: %s\n", customerID, name, email)
}
}

map的并发操作

GOLANG
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 这个程序运行的时候,由于map不是协程安全的,同一时刻只能有一个协程对map进行操作
// 所以会报错
// 可以使用sync包对map加锁或直接使用Go在1.9版本中提供的线程安全map。
package main

func main() {
GoMap := make(map[int]int)
for i := 0; i < 10000; i++ {
go writeMap(GoMap, i, i)
go readMap(GoMap, i)
}
}

func writeMap(Gomap map[int]int, key int, value int) {
Gomap[key] = value
}
func readMap(Gomap map[int]int, key int) int {
return Gomap[key]
}
GOLANG
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 加锁后的操作
// 但是加锁性能会有一定影响,所以可以用sync.Map,效率更高
package main

import "sync"

var lock sync.RWMutex

func main() {
GoMap := make(map[int]int)
for i := 0; i < 10000; i++ {
go writeMap(GoMap, i, i)
go readMap(GoMap, i)
}
}

func writeMap(Gomap map[int]int, key int, value int) {
lock.Lock()
Gomap[key] = value
lock.Unlock()
}
func readMap(Gomap map[int]int, key int) int {
lock.Lock()
value := Gomap[key]
lock.Unlock()
return value
}
GOLANG
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// sync.Map的操作,特点如下
// 内部通过冗余的数据结构降低加锁对性能的影响。
// 使用前无须初始化,直接声明即可。
// sync.Map不使用map中的方式来进行读取和赋值等操作。
// 未提供获取map数量的方法,需要遍历来获取数量
package main

import "sync"

var lock sync.RWMutex

func main() {
var GoMap sync.Map
for i := 0; i < 10000; i++ {
go writeMap(GoMap, i, i)
go readMap(GoMap, i)
}
}

func writeMap(Gomap sync.Map, key int, value int) {
// 进行线程安全的存储
Gomap.Store(key, value)
}
func readMap(Gomap sync.Map, key int) int {
// 进行线程安全的读取
value, ok := Gomap.Load(key)
if ok {
// 这里返回的是接口类型,需要转换为map的值类型
return value.(int)
}

return 0
}
 评论