![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Go
思维小刀
这个作者很懒,什么都没留下…
展开
-
Go外层的主协程可以捕获子协程的panic吗
【代码】Go外层的主协程可以捕获子协程的panic吗。原创 2024-05-03 10:57:44 · 147 阅读 · 0 评论 -
Go报错invalid operation: arr[i] (type *[]int does not support indexing)
因为go会把 * arr[i] 当作 * (arr[i])go语言调用数组指针的时候犯了个错,使用了以下方式赋值,结果报错。原创 2023-06-29 13:56:05 · 235 阅读 · 0 评论 -
Golang函数有波浪线Comment should have the following format
原创 2023-06-06 16:12:25 · 246 阅读 · 0 评论 -
Go主协程和子协程的并发执行
Golang原创 2022-06-13 13:57:06 · 336 阅读 · 1 评论 -
Go回调函数
go回调函数原创 2022-06-08 11:20:08 · 912 阅读 · 0 评论 -
Go解决slice map线程不安全的原因与方法
slice线程不安全原因程全的情况主要是在内存的扩容两个协程同时写入到同一个空间的时候就会出现竞争;最终只有一个写入sync的锁机制map不安全原因Map在进行查找、赋值、遍历、删除的操作的时候都会对hamp.flags进行标记,如果发现有标记则直接panic1. 是运用sync.RWMutex 2. 是运用sync.Map...原创 2022-05-16 20:30:54 · 416 阅读 · 0 评论 -
Golang心跳实现
客户端发送心跳请求+重试机制。重试X次失败则定义为离线服务端响应心跳请求+超时机制。超时X秒未收到心跳则离线//服务端//全局变量var ids = make(map[string]chan byte)func ResponseHeartBeat(id string) { if _, ok := ids[id]; ok { //保活心跳请求 ids[id] <- 0 } else { //上线触发器 ...原创 2022-05-16 16:24:19 · 909 阅读 · 0 评论 -
Golang并发操作中常见的死锁情形
https://www.jb51.net/article/221391.htm#_label0第一种情形:无缓存能力的管道,自己写完自己读 第二种情形:协程来晚了 第三种情形:管道读写时,相互要求对方先读/写 第四种情形:读写锁相互阻塞,形成隐形死锁...原创 2022-05-16 16:26:32 · 255 阅读 · 0 评论 -
Golang调度器runtime
Gosched:让当前线程让出 cpu 以让其它线程运行,它不会挂起当前线程,因此当前线程未来会继续执行 NumCPU:返回当前系统的 CPU 核数量 GOMAXPROCS:设置最大的可同时使用的 CPU 核数 Goexit:退出当前 goroutine(但是defer语句会照常执行) NumGoroutine:返回正在执行和排队的任务总数 GOOS:目标操作系统package mainimport ( "fmt" "runtime" "time")func main() {原创 2022-05-16 12:31:46 · 125 阅读 · 0 评论 -
Go的context:协程控制+上下文传递
并发控制方式sync.waitGroup 一个整体任务的部分任务,适用于自然结束的select+channel 灵活些但是需要设置channel,适用于一个协程环境context 适用于一个或多个协程结束context函数context.Background() 返回一个空的 Context,这个空的 Context 一般用于整个 Context 树的根节点。context.WithCancel(parent) 创建一个可取消的子 Context,然后当作参数传给 gorouti原创 2022-05-14 18:14:02 · 1590 阅读 · 0 评论 -
Gin新建一个项目的流程与函数
main文件加载配置 初始化redis mongo ck routerrouter文件加载中间件 进行校验数据 控制器中有模型的对象,原创 2022-03-10 15:55:32 · 322 阅读 · 0 评论 -
Go使用go-clickhouse库
连接func NewCkConn(ckInfo config.CkConfig) (*dbr.Connection, error) { conn, err := dbr.Open("clickhouse", "http://"+ckInfo.Username+":"+ckInfo.Password+"@"+ckInfo.Host+":"+ckInfo.Port+"/"+ckInfo.Database, nil) if err != nil { return nil, err } retur原创 2022-03-04 11:52:01 · 3745 阅读 · 1 评论 -
Go的Gin框架获取GET POST参数 两个一样的
GETbody := struct { ResourceId string `json:"resource_id" bson:"resource_id" form:"resource_id"` ResourceType string `json:"resource_type" bson:"resource_type" form:"resource_type"` }{}var err errorerr = c.Bind(&body)POSTbody := struct原创 2022-02-14 18:02:40 · 513 阅读 · 0 评论 -
Go性能测试benchmark
教程:Go benchmark 详解 - YahuiAn - 博客园练习:github中包含了 命令行的执行及其结果go-box/practice/benchmark at master · 7117/go-box · GitHub原创 2022-01-21 18:33:07 · 220 阅读 · 0 评论 -
Go之JWT详解
JWT详解 | 包包的Tech Pool原创 2022-01-20 14:09:16 · 1036 阅读 · 0 评论 -
Go之Beego原生查询QueryRow QueryRows Values ValuesList ValuesFlat RowsToMap RowsToStruct
QueryRow 是单条数据QueryRows是数组 多条数据Values返回结果集的 key => value 值ValuesList 返回结果集 sliceValuesFlat 返回单一字段的平铺 slice 数据RowsToMap// map[string]interface{}{// "total": 100,// "found": 200,// }RowsToStructfmt.Println(res.Total) // 100fmt.Pri...原创 2020-10-05 22:29:58 · 1792 阅读 · 0 评论 -
Go之Beego报错RegisterModel must be run before BootStrap
报错beego02:RegisterModel must be run before BootStrapRequest Method: GETRequest URL: /testmodelRemoteAddr: 127.0.0.1StackD:/Go/src/runtime/panic.go:969D:/gopath/src/github.com/astaxie/beego/orm/models_boot.go:308D:/gopath/src/goPractice/beego02/c.原创 2020-10-05 21:44:35 · 1346 阅读 · 0 评论 -
Go使用Convey单元测试Monkey打桩
Golang 实现 monkey patch - 简书原创 2021-12-16 20:23:24 · 871 阅读 · 0 评论 -
Go使用mgo增删改查聚合操作
包github.com/globalsign/mgo查询查询所有query := func(c *mgo.Collection) (interface{}, error) {q := bson.M{"widget_id": param.WidgetId, "chart_id": param.ChartId, "is_show": bson.M{"$ne": 0}}var items []new_panel.Metricif err := c.Find(q).All(&a...原创 2021-12-14 16:39:04 · 636 阅读 · 0 评论 -
Go原始获取Get Post方式的数据
Get defer ctx.Request.Body.Close() WidgetId := ctx.Request.URL.Query().Get("widget_id") MetricId := ctx.Request.URL.Query().Get("metric_id") ChartId := ctx.Request.URL.Query().Get("chart_id") param := new_panel.WidgetMetric{ ChartId: ChartId原创 2021-12-14 16:26:34 · 307 阅读 · 0 评论 -
Gin实际编写中间件
路由的使用router.GET("/v1/metata/metric-maer/metric-cagory", metadataHandler.Category)router.Use(middleware.CloudProduct(config.GetConfiguration()))router.PUT("/v1/metada/metic-nager/diion", metadataHandler.PutDimension)具体的代码// CloudProduct 验证云产品的权限.原创 2021-12-14 16:17:51 · 92 阅读 · 0 评论 -
Gin使用mongo进行增删改查/聚合/数据转换
包go.mongodb.org/mongo-driver/mongo查询查询Count包含了过滤+回调函数+执行三部分filter := primitive.M{}filter["monitor_ta"] = body.MonitorTypefilter["catego"] = body.categofilter["status"] = primitive.M{"$ne": DelStatus,}query := func(collection *mongo.Collection...原创 2021-12-14 16:13:34 · 847 阅读 · 0 评论 -
Go笔试题目Go与PHP分别实现方法
题目Go实现PHP实现代码package mainimport "fmt"var table = [][]int{{2, 2, 2, 0, 0, 0}, {0, 1, 5, 0, 0, 1}, {2, 1, 3, 3, 0, 0}, {3, 1, 1, 0, 0, 0}, {0, 2, 1, 0, 0, 0}}var res = map[int][]int{}var count intfunc main() { x := 0 y := 1 ...原创 2021-08-12 20:09:11 · 134 阅读 · 0 评论 -
Go使用HTTPClient发送Get Post请求
Get请求 url := c.Host + WarningNum req, err := http.NewRequest("GET", url, nil) q := req.URL.Query() q.Add("psaId", paramGet.PsaId) q.Add("paasZoneCode", paramGet.PaasZoneCode) req.URL.RawQuery = q.Encode() req.Header.Add("Content-Type", "applic原创 2021-11-05 14:53:05 · 959 阅读 · 0 评论 -
Go的string/int/int64转化
string到intint,err:=strconv.Atoi(string)string到int64int64, err := strconv.ParseInt(string, 10, 64)int到stringstring:=strconv.Itoa(int)int64到stringstring:=strconv.FormatInt(int64,10)原创 2021-11-01 19:45:24 · 2464 阅读 · 0 评论 -
Go原生获取Get Post参数的方法
GET defer ctx.Request.Body.Close() ChartId := ctx.Request.URL.Query().Get("chart_id") WidgetId := ctx.Request.URL.Query().Get("widget_id")POST defer ctx.Request.Body.Close() body, err := ioutil.ReadAll(ctx.Request.Body) if err != nil {原创 2021-09-29 20:02:01 · 529 阅读 · 0 评论 -
Gol使用mgo完成distinct与group操作
distinctquery := func(c *mgo.Collection) (interface{}, error) { q := bson.M{} var items []string err := c.Find(q).Distinct("widget_name", &items) if err != nil { return nil, err } fmt.Println(items) return items, nil }res, err :原创 2021-09-14 17:51:39 · 721 阅读 · 0 评论 -
Go使用swagger:方法说明+案例说明
https://blog.csdn.net/qq_37493556/article/details/104081138原创 2021-09-08 11:31:51 · 903 阅读 · 0 评论 -
Go安装swagger:swagger的安装与使用
https://blog.csdn.net/qq_37493556/article/details/104081138原创 2021-09-07 19:39:10 · 893 阅读 · 0 评论 -
Goland显示“Cannot resolve symbol XXX“ 进行更新IDE的索引库
菜单“File” -> “Invalidate Caches / Restart”→ “Invalidate and Restart”。原创 2021-09-07 19:39:44 · 2396 阅读 · 0 评论 -
Go的channel与map是否并发安全以及原因
channel安全:底层实现出队入队时也加锁了map不安全:加互斥锁或者读写锁原创 2021-08-12 21:25:05 · 1031 阅读 · 0 评论 -
Golang类型转化方法汇总
// 访问嵌套的值需要一系列的转化。strs := dat["strs"].([]interface{})str1 := strs[0].(string)fmt.Println(str1)原创 2021-08-04 10:53:36 · 97 阅读 · 0 评论 -
Golang的Int8 Int16 Int32 Int64区别
位(bit):二进制数中的一个数位,可以是0或者1,是计算机中数据的最小单位字节(Byte B):计算机中数据的基本单位,每8位组成一个字节字(Word):两个字节称为一个字。汉字的存储单位都是一个字。ASCIIS码1个ASCII码 = 一个字节 UTF-8编码1个英文字符 标点 = 1个字节1个中文(含繁体)标点 = 3个字节Unicode编码1个英文字符 标点 = 2个字节1个中文(含繁体)标点 = 2个字节Int8, 等于Byte, 占1个字节原创 2021-08-03 16:12:59 · 1508 阅读 · 0 评论 -
Go的遍历map与slice的区别:map无序slice有序
for range遍历map,发现打印出来的map是无序的【随机播种】for range遍历切片,是有序的。原创 2021-05-17 22:11:21 · 1966 阅读 · 0 评论 -
Go微服务报错protoc-gen-go: unable to determine Go import path for
问题$ protoc --go_out=./ hello.protoprotoc-gen-go: unable to determine Go import path for "hello.proto"Please specify either: • a "go_package" option in the .proto source file, or • a "M" argument on the command line.See https://deve.原创 2021-05-14 23:11:10 · 12727 阅读 · 1 评论 -
Golang手册与常用包手册
http://www.topgoer.com/https://studygolang.com/pkgdoc原创 2021-04-25 10:26:37 · 408 阅读 · 0 评论 -
Goland提示Unresolved reference ***错误解决
原因goland没有创建文件索引,导致包里面的函数找不到了,方法重新安装了go1.16.3.windows-amd64.msi,goland会新建文件索引,就正常显示函数名字了参考https://blog.csdn.net/zhouzme/article/details/90201930...原创 2021-04-17 11:36:26 · 1254 阅读 · 0 评论 -
Go进程/线程/协程:单元 空间资源 切换 共享
OS操作系统分为操作与资源两部分,操作就是方法,资源就是硬盘资源,合称为操作系统。因为是一个系统,任务很多很繁杂,所以根据软件不同进行分解,一个软件就是一小块,就是进程。进程比喻:进程就是应用程序的启动实例,一个软件一个进程,例如我们运行一个游戏,打开一个软件,就是开启了一个进程。过程:启动一个软件会把此进程代码(就是方法集,方法集包括生产者消费者函数)注入到cpu的寄存器中,...原创 2019-09-07 10:32:00 · 422 阅读 · 0 评论 -
Goland如何断点调试Debug
第一步:进入编辑模式第二步:开始进行编辑第三步:就可以进行调式了原创 2020-10-21 23:12:31 · 13936 阅读 · 2 评论 -
Go获取年月第一天与最后一天 获取时间戳
func main() { now := time.Now() currentYear, currentMonth, _ := now.Date() currentLocation := now.Location() //time.Time格式 firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation) lastOfMonth := firstOfMonth.AddDate(0, 1,.原创 2020-10-19 17:13:17 · 1871 阅读 · 0 评论