![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Go
ezreal_pan
这个作者很懒,什么都没留下…
展开
-
go mod模式下,import gitlab中的项目
第一步:创建一个公用包的项目,比如,core。这里有个点需要注意,不能直接用go mod init进行初始化,否则,引用该包会报路径错误的问题。需要通过这个命令进行初始化(init后面的内容实是ssh链接去掉git@)。执行完之后,push到gitlab上。公用包创建的流程到此就完成了。接下来就是引用包需要做些一些配置。第二步:第三步:需要设置两个环境变量。GOPRIVATE。原创 2024-05-23 15:55:36 · 1021 阅读 · 0 评论 -
gorm log with traceId 打印带有traceId信息的日志,通过context实现
于是乎,我们就希望有一个全局的traceId可以把一个请求过程中经过的所有链路的关键信息串联起来,这样的话在检索日志的时候可以带来极大的方便,根据traceId把整个链路上的日志全部打印出来。在初始化日志的地方,传从Context中获取对应参数的函数,比如,从context中接受traceId。我们首先需要了解gorm日志打印是如何实现的,任意找一个sql执行方法进去,比如,查询的方法。我们回到前面打印日志的地方,看打印日志的方法,打印日志的Trace方法是这个接口下的一个方法。原创 2024-05-15 16:32:10 · 481 阅读 · 0 评论 -
http请求头导致了dial tcp:lookup xxxx on 10.43.0.10:53 no sunch host
事实证明人有的时候也不能太偷懒,太偷懒容易给自己埋坑。原创 2024-04-11 15:42:19 · 527 阅读 · 0 评论 -
周期时间计算方法《二》
【代码】周期时间计算方法《二》原创 2022-08-24 17:13:14 · 391 阅读 · 1 评论 -
周期时间计算方法
【代码】周期时间计算方法。原创 2022-08-23 17:29:56 · 848 阅读 · 0 评论 -
golang编译出现问题
项目中引入了第三方包(github.com/jinzhu/copier),编译的时候出现了go: github.com/jinzhu/copier@v0.3.5: verifying go.mod: github.com/jinzhu/copier@v0.3.5/go.mod: reading http://192.168.24.11:3000/sumdb/sum.golang.org/lookup/github.com/jinzhu/copier@v0.3.5: 502 Bad Gatewaygo:原创 2022-07-01 09:55:33 · 1014 阅读 · 0 评论 -
golang 关于引用和指针的深拷贝的问题
问题现象定义一个对象,对象中嵌套定义了map类型和一个对象,复制原来的对象生成新的对象,改变新对象中map的值和嵌套的对象的值会导致原来的对象的值也被改变。现象模拟package mainimport ( "encoding/json" "fmt")type StudyCopy struct { Name string Reference map[string]interface{} Nested *Nested}type Nested struct {原创 2022-03-30 14:16:25 · 1588 阅读 · 0 评论 -
golang 匿名结构体数组对应的json结构体记录
或者原创 2021-12-15 16:43:40 · 226 阅读 · 0 评论 -
golang beego使用踩坑记录
conf文件中,数组型数据不能放在前面,会导致其他的如string型数据解析为空这种数组型数据要放到文件的最后。一不小心引入了bug,花了一天时间才往配置文件方向考虑,终于定位到问题了...原创 2021-12-02 10:48:05 · 115 阅读 · 0 评论 -
基于mongodb的进程锁设计
进程锁设计逻辑:/*数据库脚本use cloudeventserver;//----------------------------//Collectionstructureforlock//----------------------------db.getCollection("lock").drop();db.createCollection("lock");db.getCollection("lock").createIndex({"lock_ke...原创 2021-07-26 14:49:40 · 920 阅读 · 0 评论 -
各浏览器下载文件名不乱码的解决办法
转自:https://segmentfault.com/a/1190000005994758/转载 2021-06-08 18:54:43 · 1355 阅读 · 0 评论 -
golang 利用反射写excel
excel操作接口封装package excelimport ( "context" "fmt" "reflect" "strings" "github.com/360EntSecGroup-Skylar/excelize")type Field struct { Value interface{}}//excel操作接口type IExcelHandler interface { // 获取处理器标识名,用途有: // 1.根据标识名替换某个处理器 // 2.打原创 2021-05-20 10:02:04 · 280 阅读 · 0 评论 -
golang 结构体数组,多条件排序
需要重写sort包下的Interface接口方法。如下为示例:package mainimport ( "bytes" "fmt" "io/ioutil" "reflect" "sort" "time" "golang.org/x/text/encoding/simplifiedchinese" "golang.org/x/text/transform")const ( FORMAT_DATETIME = "2006-01-02 15:04:05")func原创 2021-05-14 10:09:24 · 1693 阅读 · 0 评论 -
Golang 中函数作为值与类型
参考博客:https://blog.csdn.net/zchdjb/article/details/90725435package mainimport "fmt"func main() { fc := func() { fmt.Println("print fc") } fmt.Printf("fc:%T\n", fc) fc() myMsg := func(msg string) { fmt.Println("msg:", msg) } fmt.Printf("my.转载 2021-04-28 15:17:39 · 71 阅读 · 0 评论 -
golang mongodb 涉及到多字段排序
涉及到多字段排序的操作,代码结构如下,详情见注释:func makeGoodsOptions(page, pageSize int64) *options.FindOptions { //分页 limit := pageSize skip := (page - 1) * pageSize //排序 map无序,因此这样子会报错 // sortMap := make(map[string]interface{}, 0) // sortMap["update_time"] = -1 //原创 2021-04-23 16:47:31 · 3503 阅读 · 2 评论 -
multi-key map passed in for ordered parameter sort
记录一次阅读源码的过程。在使用golang mongo 查询的过程中,使用了sort进行排序,使用了多个字段进行排序。结果,返回multi-key map passed in for ordered parameter sort 这个错误。首先上网查了一下这个错误,发现没有人遇到过这样的问题。查询中,使用了find方法,以及排序,如下图所示。我看了一下源码。在源码中找到Find()方法。定位到Find()方法中,sort参数解析。从源码看,的确是不支持多个原创 2021-04-23 14:39:53 · 2274 阅读 · 2 评论 -
err:pq: at or near “?“
CockroachDB 写数据报了这个错误,是因为postgres占位符是$n,不是?参考文档:https://pkg.go.dev/github.com/lib/pqpackage mainimport ( "bytes" "context" "encoding/base64" "encoding/binary" "errors" "fmt" "log" "net/http" "os" "os/signal" "strconv" "strings" "sync"原创 2021-03-26 14:41:58 · 158 阅读 · 0 评论 -
StructToMap
package mainimport ( "fmt" "reflect" "github.com/fatih/structs")func main() { type User struct { Name string } type Server struct { Name string `json:"name,omitempty"` ID int Enabled bool Users []User `json:"users"` //htt.原创 2021-03-11 13:45:19 · 202 阅读 · 0 评论 -
golang interface to struct
package mainimport ( "errors" "fmt" "reflect")// func SetField(obj interface{}, name string, value interface{}) error {// structValue := reflect.ValueOf(obj).Elem()// structFieldValue := structValue.FieldByName(name)// if !structFieldValue..原创 2021-02-02 16:51:39 · 305 阅读 · 0 评论 -
golang interface to struct
package mainimport ( "errors" "fmt" "reflect")func SetField(obj interface{}, name string, value interface{}) error { structValue := reflect.ValueOf(obj).Elem() structFieldValue := structValue.FieldByName(name) if !structFieldValue.IsValid() {.转载 2021-02-02 16:24:47 · 382 阅读 · 0 评论 -
golang struct to map
/*This function will help you to convert your object from struct to map[string]interface{} based on your JSON tag in your structs.Example how to use posted in sample_test.go file.*/func structToMap(item interface{}) map[string]interface{} { res := m.原创 2021-02-01 16:59:30 · 232 阅读 · 0 评论 -
Error #01: invalid character ‘\x1f‘ looking for beginning of value
package serverimport ( "bytes" "compress/gzip" "encoding/json" "fmt" "io" "io/ioutil" "net/http" "strconv" "strings" "time" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "next-stage.com.cn/mercury-backend/api" "next-stage.com.c.原创 2020-10-22 10:04:30 · 2643 阅读 · 0 评论 -
ListToTree golang
// Team ...type Team struct { //Core ID int64 `json:"id,string" db:"id"` ClusterID int64 `json:"clusterId,string" db:"cluster_id"` ParentID int64 `json:"parentId,string" db:"parent_id"` CreatedAt int64 `json:"createdAt,string" db:"cre..原创 2020-10-15 14:37:26 · 253 阅读 · 0 评论 -
golang 透明代理
package mainimport ( "bytes" "encoding/json" "fmt" "io/ioutil" "log" "net/http" "net/http/httputil" "net/url" "os")type requestPayloadStruct struct { ProxyCondition string `json:"proxy_condition"`}// Get env var or defaultfunc getEnv(ke.原创 2020-06-03 18:27:36 · 1038 阅读 · 3 评论 -
golang gin download and upload file
func main() { router := gin.Default() router.GET("/api/v1/attachments/:file",DownloadAttachmentHandler) router.POST("/api/v1/attachment/files", UploadAttachmentFileHandler) // Listen and serve on 0.0.0.0:8080 r.Run(":8080")}// Uplo.原创 2020-06-03 18:10:45 · 3295 阅读 · 0 评论 -
How to log response and request body in gin
func main() { r := gin.New() r.Use(UploadRetrievalLog()) router.GET("/api/v1/ping", func(c *gin.Context) { c.String(200, "pong") }) // Listen and serve on 0.0.0.0:8080 r.Run(":8080")}type bodyWriter struct { gin.ResponseWrite.原创 2020-05-28 13:40:45 · 1258 阅读 · 0 评论 -
go_package 别名问题记录
syntax = "proto3";package entity.v5;option go_package = "next-stage.com.cn/apatodon/protocol/entity/v5;entity.v5";import "google/api/annotations.proto";import "google/protobuf/wrappers.prot...原创 2020-04-21 16:41:09 · 4707 阅读 · 1 评论 -
golang list to tree
// You can edit this code!// Click here and start typing.package mainimport ( "encoding/json" "log")//树type Tree struct { List map[int]*Node Children map[int]Node Parents map[int]No...原创 2020-03-25 10:20:04 · 1363 阅读 · 0 评论 -
golang遍历FTP文件夹
package ftpimport ( "fmt" "github.com/jlaffaye/ftp")//SourceManager ...type SourceManager struct { conn *ftp.ServerConn}// EntryHandler 遍历ftp目录时的文件handler// EntryHandler ...type EntryH...原创 2019-11-08 11:04:34 · 1675 阅读 · 0 评论 -
golang package学习总结
省略调用(不建议使用):package mainimport . "fmt"func main() { // 调用的时候只需要Println(),而不需要fmt.Println() Println("省略调用")}使用别名调用包中的函数:package mainimport f "fmt"func main() { //使用别名调用包中的函数,调用的时候只需...原创 2019-04-10 10:35:21 · 113 阅读 · 0 评论 -
golang interface学习总结
package mainimport ( "fmt" "math")//interface可以实现泛型编程//简单的说,interface是一组method的组合//interface 可以实现泛型编程type geometry interface { calculatearea() float64 calculateperim() float64}type rect...原创 2019-04-10 10:05:03 · 177 阅读 · 0 评论 -
理解go的function types
转自:作者:咕咕鷄链接:https://www.jianshu.com/p/fc4902159cf5来源:简书今天看Martini文档,其功能列表提到完全兼容http.HandlerFunc接口,就去查阅了Go: net/http的文档,看到type HandlerFunc这部分,顿时蒙圈了。由于之前学习的时候没有关注过function types的知识点,就Google了一些文章,才...转载 2019-03-26 15:26:09 · 139 阅读 · 0 评论 -
golang模板语法简明教程
转自:https://www.cnblogs.com/Pynix/p/4154630.html【模板标签】模板标签用"{{"和"}}"括起来【注释】{{/* a comment */}}使用“{{/*”和“*/}}”来包含注释内容【变量】{{.}}此标签输出当前对象的值{{.Admpub}}表示输出Struct对象中字段或方法名称为“Admpub...转载 2019-03-07 14:27:49 · 556 阅读 · 0 评论 -
Golang Template 简明笔记
后端分离的Restful架构大行其道,传统的模板技术已经不多见了。实际上只是渲染的地方由后端转移到了...转载 2019-03-07 13:49:54 · 1716 阅读 · 0 评论 -
cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
安装go-sqlite的时候,在编译的时候,遇到,cc1.exe: sorry, unimplemented: 64-bit mode not compiled in这个错误。go编译c 环境配置,要采用64位的mingw编译器才可以编译成功下载地址:http://sourceforge.net/projects/mingw-w64/files/Toolchains%2...原创 2019-03-06 17:05:32 · 1103 阅读 · 0 评论 -
golang 模板template自定义函数用法示例以及注意事项
package mainimport ( "html/template" "net/http" "time")type User struct { Username, Password string RegTime time.Time}//这个函数的名字要大写,要不然模板中无法调用这个函数func ShowTime(t time.Time, fo...原创 2019-03-08 17:04:26 · 5240 阅读 · 0 评论 -
GRPChttp请求遇到的异常(all SubConns are in TransientFailure, latest connection error: connection closed)
func main() { stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) go func() { lis, err := net.Listen("tcp", "localhost:8181") if...原创 2019-04-16 09:59:13 · 10219 阅读 · 1 评论 -
Go 1.9 的类型别名
转自:https://colobu.com/2017/06/26/learn-go-type-aliases/目录[−]类型别名 类型命名和类型声明的区别 类型循环 可导出性 方法集 byte 和 rune 类型 参考资料如你所知, 类型别名(type aliases) 最终还是加入到Go 1.9中, Go 1.9 beta2今天已经发布了, 正式版预计8月初发布, 是时候...转载 2019-05-09 18:21:26 · 249 阅读 · 0 评论 -
golang 初学推荐的几个网站
Go by example:https://gobyexample.com/ effective go:https://golang.org/doc/effective_go.htmlGO 101:https://go101.org/article/101.html原创 2019-05-09 18:25:26 · 671 阅读 · 0 评论 -
golang fatal error: all goroutines are asleep - deadlock!
转自:https://www.cnblogs.com/ghj1976/p/4295013.htmlchannel默认上是阻塞的,也就是说,如果Channel满了,就阻塞写,如果Channel空了,就阻塞读。阻塞的含义就是一直等到轮到它为止。单有时候我们会收到 fatal error: all goroutines are asleep - deadlock! 异常,这是如何呢?代码例子:...转载 2019-07-19 15:40:54 · 3079 阅读 · 0 评论