何时使用 new() 和 make()

  • 切片、映射和通道,使用 make
  • 数组、结构体和所有的值类型,使用 new
2021/07/03 posted in  Golang

Golang 在 Mac、Linux、Windows 下如何交叉编译

Golang 支持交叉编译,在一个平台上生成另一个平台的可执行程序,最近使用了一下,非常好用,这里备忘一下。

Mac 下编译 Linux 和 Windows 64位可执行程序

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

Linux 下编译 Mac 和 Windows 64位可执行程序

CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

Windows 下编译 Mac 和 Linux 64位可执行程序

SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build main.go

SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build main.go

GOOS:目标平台的操作系统(darwin、freebsd、linux、windows)
GOARCH:目标平台的体系架构(386、amd64、arm)
交叉编译不支持 CGO 所以要禁用它

上面的命令编译 64 位可执行程序,你当然应该也会使用 386 编译 32 位可执行程序
很多博客都提到要先增加对其它平台的支持,但是我跳过那一步,上面所列的命令也都能成功,且得到我想要的结果,可见那一步应该是非必须的,或是我所使用的 Go 版本已默认支持所有平台。

2018/07/24 posted in  Golang

开源库

UUID生成 : https://github.com/dreamans/guuid
Golang动态口令库:https://github.com/xlzd/gotp
复杂JSON参数解析库: https://github.com/bitly/go-simplejson
时间解析库carbon:https://github.com/golang-module/carbon

仓库资源

如果在 go get 或者 glide get 的时候提示以下错误:

[WARN] Unable to checkout golang.org/x/crypto
[ERROR] Update failed for golang.org/x/crypto: Cannot detect VCS
[WARN] Unable to checkout golang.org/x/net
[ERROR] Update failed for golang.org/x/net: Cannot detect VCS
[ERROR] Failed to install: Cannot detect VCS
Cannot detect VCS

解决方法是设置golang.org镜像到github.com/golang

glide mirror set https://golang.org/x/mobile https://github.com/golang/mobile --vcs git
glide mirror set https://golang.org/x/crypto https://github.com/golang/crypto --vcs git
glide mirror set https://golang.org/x/net https://github.com/golang/net --vcs git
glide mirror set https://golang.org/x/tools https://github.com/golang/tools --vcs git
glide mirror set https://golang.org/x/text https://github.com/golang/text --vcs git
glide mirror set https://golang.org/x/image https://github.com/golang/image --vcs git
glide mirror set https://golang.org/x/sys https://github.com/golang/sys --vcs git
glide mirror set https://golang.org/x/net/websocket https://github.com/gorilla/websocket --vcs git
2018/03/22 posted in  Golang

在golang中使用mgo多条件查询

今天被mgo凶残的语法折腾跪了

一般做简单查询,是这样写的:

collection := mgodbcontroller.GetMdb().C(mgodbcontroller.USER_WALLET_GS_LOG)//获取操作对象
//根据用户手机号 倒序查询前100个 存入slice中
if err := collection.Find(&bson.M{"uphone": phonenum}).Sort("-initimesamp").Limit(100).All(&historys); err == nil {

	for i, _ := range historys {
		fmt.Println("从mgo查询到的数据--->", historys[i])
		ms = append(ms, &historys[i])
	}

} else {
	fmt.Println("查询历史", err)
	logUtils.GetLog().Error("查询历史", err)
}

现在需要查询这些用户中,手机号为18112345678,同时历史信息是reasonareasonb的历史,如果直接用命令来查询,那简单的很,这样写就好:

db.userwalletgslog.find({"uphone":"18112345678","reason":{"$in":["reasona","reasonb"]}})

接下来,苦逼的问题来了,要用golang调用mgo来实现上面这个语句,咋整
百度N久,找到个这么写的:(链接是 http://blog.csdn.net/varding/article/details/17200299 )

c.Find(bson.M{"name": bson.M{"$in": []string{"Jimmy Kuu", "Tracy Yu"}}}).All(&users)

他的这个写法其实有点问题,至少我的程序里跑不起来,经过了长~~~~~~时间的测试:

答案在此:

err := collection.Find(&bson.M{"uphone": "18112345678", "reason": &bson.M{"$in": []string{"reasona", "reasonb"}}}).All(&historys)

看到了嘛,看到了嘛,这个坑爹的&bson.M 我真心TMD啊

2017/08/24 posted in  Golang