需要 MongoDB 3.6 及以上, 需要 ReplicaSet 模式。
监听一个字段的变化:func watch(coll *mongo.Collection) {match := bson.D{{"operationType", "update"},{"updateDescription.updatedFields.name", bson.D{{"$exists", true}}}}coll.Watch(context.Background(), mongo.Pipeline{{{"$match", match}}},options.ChangeStream().SetFullDocument(options.UpdateLookup))
}监听两个字段的变化:func watch(coll *mongo.Collectio...
Golang交叉编译1. 编译环境宿主开发平台为:
Linux wanhy-ubuntu 5.3.0-62-generic
#56~18.04.1-Ubuntu SMP Wed Jun 24 16:17:03 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux目标执行平台为:
Linux pilotx-du 4.14.98+gebeb3d5
#1 SMP PREEMPT Wed Aug 21 10:17:13 UTC 2019 aarch64 GNU/Linux
2. 常规编译2.1 Go Build使用2.1.1 GOOS变量The operating system for which to compile code.
Examples are linux, darwin, windows, ...
1 简介最近两年Golang发展迅猛,大有比肩C++、Java等主流语言、成为未来十年最为流行的编程语言之势。在开源社区中,热火朝天的Docker、Kubernetes、CoreOS生态圈都采用Golang开发,便是Golang大热的最好例证。Golang之所以备受推崇,一方面得益于其特有的并发编程模型,一方面得益于其更为简约抽象的编程哲学(接口、OOP等特性)。作为分布式领域的开发者,不能不关注Golang。2 安装环境:Ubuntu 14.04sudo apt-get install golan...
思路不太好理解,请用断点package mainimport"fmt"type tree struct {value intleft, right *tree
}func Sort(values []int) {var root *treefor _, v := range values {root = add(root, v)}appendValues(values[:0], root)
}func appendValues(values []int, t *tree) []int {if t != nil {values = appendValues(values, t.left)values = append(values, t.value)values = appendValues(values, t.right)}return values
}...
在同一个问题上栽了两次,以后碰到cookie出问题多半都是因为这个。Request.Cookie(name)取Cookie的时候,返回值只有name和valuecookie.gocookies = append(cookies, &Cookie{Name: name, Value: val})可以看到只返回了Name和Value两个值,大概是为了所谓性能吧。于是在拦截器里读取当前Cookie并重设时间的时候,Path和时间值都要重设一下。不然原来根目录下的Cookie还在,而新的Cookie就根据某些规则种到不知道哪里了,还有可能导致...
目录Golang - 异常处理1. 抛异常和处理异常2. 返回异常Golang - 异常处理1. 抛异常和处理异常package mainimport "fmt"//系统抛异常
func test0111() {a := [5]int{0, 1, 2, 3, 4}a[1] = 123//a[10] =456index := 10//index out of rangea[index] = 456
}//自己抛
func test222() {getCircleArea(-5)
}func getCircleArea(radius float32) (area float32) {if radius < 0 {//自己抛异常panic("您的智商已下线,半径不能为负数")}re...
安装Golang:1. 下载安装包https://golang.google.cn/dl/我这里使用压缩包,下载后解压到D盘(自定义)。2. 添加环境变量:把解压后的bin目录添加到环境变量中 第一个程序:1. 新建一个test.go文件,编写如下内容:package mainimport "fmt"func main() {fmt.Println("Hello, World!")
}2. 进入 cmd 运行 go run test.go ,就会输出:Hello, World! 到此,Golang的第一个程序就完成了! 原文:https://www.cnblogs.com/delav/p/993...
Golang是最好的编程语言原文:http://www.cnblogs.com/lidashuang/p/3535128.html
本篇文章内容来源于Golang核心开发组成员Andrew Gerrand在Google I/O 2014的一次主题分享“Testing Techniques”,即介绍使用Golang开发 时会使用到的测试技术(主要针对单元测试),包括基本技术、高级技术(并发测试、mock/fake、竞争条件测试、并发测试、内/外部测 试、vet工具等)等,感觉总结的很全面,这里整理记录下来,希望能给大家带来帮助。原Slide访问需要自己搭梯子。另外这里也要吐槽一 下:Golang官方站的slide都是以...
https://nanxiao.gitbooks.io/golang-101-hacks/content/posts/select-operation.html---------------------------------------------------------------------------------Select operationGo‘s select operation looks similar to switch, but it‘s dedicated to poll send and receive operations channels. Check the following example:package mainimport ("fmt""time"
)func main() {ch1 := make(chan int)ch2 := make(ch...
原文:https://www.cnblogs.com/CRayFish07/p/11580043.html
单元测试是质量保证十分重要的一环,好的单元测试不仅能及时地发现问题,更能够方便地调试,提高生产效率,所以很多人认为写单元测试是需要额外的时间,会降低生产效率,是对单元测试最大的偏见和误解go 语言原生支持了单元测试,使用上非常简单,测试代码只需要放到以 _test.go 结尾的文件中即可。golang的测试分为单元测试和性能测试,单元测试的测试用例以 Test 开头,性能测试以 Benchmark 开头举个例子实现排列组合函数对应的...
本文记载下了golang插件的用法, 原文:https://code.tutsplus.com/tutorials/writing-plugins-in-go--cms-29101 这个例子使用了两个排序: 快速排序和冒泡排序,不追求算法的高效,只为了说明插件的生成和使用。而且只在linux下,build和运行.(windows环境没有测试过, 原文在windows下要使用docker)目录:plugins├── bubble_sort│ └── bubble_sort_plugin.go 冒泡排序├── bubble_sort_plugin.so 编译后的插件...
学习golang难免需要分析源码包中一些实现,下面就来说说container/heap包的源码 heap的实现使用到了小根堆,下面先对堆做个简单说明1. 堆概念 堆是一种经过排序的完全二叉树,其中任一非终端节点的数据值均不大于(或不小于)其左孩子和右孩子节点的值。 最大堆和最小堆是二叉堆的两种形式。 最大堆:根结点的键值是所有堆结点键值中最大者。 最小堆:根结点的键值是所有堆结点键值中最小者。2. heap 树的最小元素...
1package main2 3import (4"fmt" 5"go.matrix-go1" //比较有名的关于Matrix在golang中的方法库 6"strings" 7)8 9func main() {
1011//matrix12 s := `[123;456]`
13//用数组字符串生成matrix14 a, err := matrix.ParseMatlab(s)
1516if err != nil {
17 fmt.Println("?", err)
18 }
1920//修改值21 a.Set(0, 0, 50)
22//给定行列位置点的值23 fmt.Println(a.Get(0, 0))
24//50
2526//列值27 fmt....