【Go】开发中遇到的坑——持续更新

关于CGo多语言编译

问题出现在将openCV封装到go语言的时候。在编译时需要设置

CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o xxx main.go

将第一项的参数CGO_ENABLED设置为1,否则会出现

vendor/gocv.io/x/gocv/dnn_ext.go:9:28: undefined: Mat
vendor/gocv.io/x/gocv/dnn_ext.go:16:12: undefined: NewMatWithSize
vendor/gocv.io/x/gocv/dnn_ext.go:42:2: undefined: Resize
vendor/gocv.io/x/gocv/dnn_ext.go:42:38: undefined: InterpolationDefault
vendor/gocv.io/x/gocv/dnn_ext.go:45:3: undefined: CvtColor
vendor/gocv.io/x/gocv/dnn_ext.go:48:15: undefined: NewMat

undefined的问题

关于Go中加入vendor

git无法上传vendor的问题

加入失败时首先检查要加入的包是否含有.git文件夹,存在的话将它删除,再次尝试。

如还是失败,则将整个vendor全部删除,再次全部加入。

关于go寻包顺序的问题

先找本地vendor,然后是$GOPATH/src下的包。

关于goMod的问题

如果已经将所有的包都放入vendor中,可以删除go.mod文件。

在Go中使用中间件时的坑

当用中间件处理错误时,不能使用Bind方法而应该使用ShouldBind方法,使用的后果是当出现错误时Bind会报错到AbortWithStatus,这个函数会调用c.Writer.WriteHeadNow,将responseWriter的size从-1置成0.从而中间件在处理错误时

c.Writer.Written()得不到正确的结果。
原文地址:https://www.cnblogs.com/Ryan16231112/p/12055098.html