暴露的变量必须用var定义,不能用const定义
// main.go var VERSION = "unknow" var SHA = "unknow" var BUILD_TIME = "unknow" ... func main () { app := &cli.App{ Version: VERSION + "\r\nsha: " + SHA + "\r\nbuild time: " + BUILD_TIME, ... } Makefile
tag?=v0.0.5 DATE?=$(shell date +%FT%T%z) VERSION_HASH = $(shell git rev-parse HEAD) LDFLAGS='-X "main.VERSION=$(tag)" -X "main.SHA=$(VERSION_HASH)" -X "main.BUILD_TIME=$(DATE)"' build: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags $(LDFLAGS) -o wellcli main.go 执行make build, 产生的二进制文件,就含有注入的信息了。
-ldflags '[pattern=]arg list' arguments to pass on each go tool link invocation....
FROM golang:1.16.2 as builder ENV GO111MODULE=on GOPROXY=https://goproxy.cn,direct WORKDIR /app COPY . . RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build . FROM scratch WORKDIR /app COPY --from=builder /app/your_app . # 配置时区 COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo ENV TZ=Asia/Shanghai EXPOSE 8080 ENTRYPOINT ["./your_app"]
web框架 https://github.com/gofiber/fiber http client https://github.com/go-resty/resty mock https://github.com/jarcoal/httpmock 项目结构 https://github.com/golang-standards/project-layout 环境变量操作 https://github.com/caarlos0/env https://github.com/kelseyhightower/envconfig 测试框架 https://github.com/stretchr/testify 日志框架 https://github.com/uber-go/zap html解析 https://github.com/PuerkitoBio/goquery cli工具 https://github.com/urfave/cli 各种库大全集 https://github.com/avelino/awesome-go 终端颜色 https://github.com/fatih/color 剪贴板 https://github.com/atotto/clipboard 数据库驱动 https://github.com/go-sql-driver/mysql 热重载 https://github.com/cosmtrek/air 时间处理 https://github.com/golang-module/carbon 错误封装 https://github.com/pkg/errors 结构体转二进制 https://github.com/lunixbochs/struc VIM智能补全提示 需要安装coc-go, 还有vim-go
Error EXTRA *mysql.MySQLError=Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘? ( 然而我仔细看了看sql语句,没有看出来究竟哪里有sql报错。
然而当我把作为placeholder的问号去掉,直接用表的名字,sql是可以直接执行的。我意识到这个可能是和placeholder有关。
搜索了一下,看到一个链接 https://github.com/go-sql-driver/mysql/issues/848
Placeholder can’t be used for table name or column name. It’s MySQL spec. Not bug of this project.
大意是说,placeholder是不能作为表名或者列名的。
在mysql关于prepared文档介绍中,在允许使用prepared的语句里,没有看到create table可以用placeholder https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html
prepared语句的优点有以下几个
优化查询速度 防止sql注入 但是也有一些限制
不是所有语句都能用prepared语句。常见的用法应该是作为select where之后的条件,或者INSERT语句之后的值 不支持一个sql中多条查询语句的形式
本来打算用gdb调试的,看了官方的文档https://golang.org/doc/gdb, 官方更推荐使用delve这个工具调试。
我的电脑是linux, 所以就用如下的命令安装。
go install github.com/go-delve/delve/cmd/dlv@latest
我要调试的并不是一个代码而是一个测试的代码。
当执行测试的时候报错的位置是xxx/demo/demo_test.go, 200行
dlv test moduleName/demo > b demo_test.go:200 # 在文件的对应行设置端点 > bp # print all breakpoint > c # continue to exe > p variableName
在线书籍 《Go语言原本》https://golang.design/under-the-hood/ 《Golang修养之路》https://www.kancloud.cn/aceld/golang 《Go语言高性能编程》https://geektutu.com/post/high-performance-go.html 《7天用Go从零实现Web框架Gee教程》https://geektutu.com/post/gee.html 博客关注 https://carlosbecker.com/ https://www.alexedwards.net/blog https://gobyexample.com/ 文章收藏 https://carlosbecker.com/posts/env-structs-golang https://www.alexedwards.net/blog/json-surprises-and-gotchas https://www.alexedwards.net/blog/how-to-manage-database-timeouts-and-cancellations-in-go https://www.alexedwards.net/blog/custom-command-line-flags https://www.alexedwards.net/blog/how-to-properly-parse-a-json-request-body https://www.alexedwards.net/blog/working-with-redis https://www.alexedwards.net/blog/organising-database-access https://www.alexedwards.net/blog/interfaces-explained
1. 如何安装go 本次安装环境是win10子系统 ubuntu 20.04
打开网站 https://golang.google.cn/dl/
选择合适的最新版的连接
cd mkdir download cd download wget https://golang.google.cn/dl/go1.16.3.linux-amd64.tar.gz tar -C /usr/local -xvf go1.16.3.linux-amd64.tar.gz 因为我用的是zsh 所以我在~/.zshrc中,将go的bin目录加入到PATH中 export PATH=$PATH:/usr/local/go/bin 保存.zshrc之后 source ~/.zshrc ➜ download go version go version go1.16.3 linux/amd64 2. go proxy设置 Go 1.13 及以上(推荐)
打开你的终端并执行
go env -w GO111MODULE=on go env -w GOPROXY=https://goproxy.cn,direct 3. go get 下载的文件在哪? 检查 go env
GOPATH="/Users/wangdd/go” /Users/wangdd/go/pkg/mod total 0 drwxr-xr-x 4 wangdd staff 128B Sep 14 09:17 cache drwxr-xr-x 8 wangdd staff 256B Sep 14 09:17 github....