-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
1,191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ | |
|
||
使用索引规范 | ||
最终索引结构为 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
1. elasticsearch text字段排序报错解决, 一般使用数字排序 | ||
|
||
问题表象 | ||
``` | ||
Fielddata is disabled on text fields by default. Set fielddata=true on [region] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. | ||
``` | ||
|
||
解决方式有两种: | ||
|
||
(1) 使用`region.keyword`进行聚合,排序 | ||
```js | ||
{ | ||
sort: { | ||
't.keyword': { | ||
order: 'desc' | ||
} | ||
} | ||
} | ||
``` | ||
(2) 将排序字段的`fileddata`设置为true | ||
```js | ||
{ | ||
"properties": { | ||
"t":{ | ||
"type": "text", | ||
"fielddata": true | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
### 分析defer和panic执行顺序 | ||
```go | ||
func main() { | ||
deferCall() | ||
} | ||
|
||
func deferCall() { | ||
defer func() { fmt.Println("打印前") }() | ||
defer func() { fmt.Println("打印中") }() | ||
defer func() { fmt.Println("打印后") }() | ||
|
||
panic("触发异常") | ||
} | ||
|
||
``` | ||
1. `defer`本身遵循先进后出的顺序, 代码抛出的`panic`如果在所有的defer中都不使用`recover`恢复, 则直接退出程序, 但任然打印数据 | ||
2. 如果手动使用`os.Exit()`退出, 则`defer`不执行 | ||
|
||
### 分析defer和return执行顺序 | ||
```go | ||
func main() { | ||
fmt.Println(double1(5)) | ||
fmt.Println(double1(6)) | ||
fmt.Println() | ||
fmt.Println(double2(5)) | ||
fmt.Println(double2(6)) | ||
} | ||
|
||
// 匿名返回 | ||
// 加倍参数,若结果超过 10 则还原 | ||
func double1(v1 int) int { | ||
var v2 int | ||
defer func() { | ||
if v2 > 10 { | ||
v2 = v1 // v2 不会被修改 | ||
} | ||
}() | ||
|
||
v2 = v1 * 2 | ||
return v2 | ||
} | ||
|
||
// 有名返回 | ||
func double2(v1 int)(v2 int) { | ||
// v2 与函数一起被声明,在 defer 中能被修改 | ||
defer func() { | ||
if v2 > 10 { | ||
v2 = v1 // v2 被修改 | ||
} | ||
}() | ||
|
||
v2 = v1 * 2 | ||
return | ||
} | ||
``` | ||
|
||
注意 return var 会分为三步执行: | ||
|
||
return 语句为 var 赋值 | ||
|
||
匿名返回值函数:先声明,再赋值 | ||
有名返回值函数:直接赋值 | ||
检查是否存在 defer 语句:逆序执行多条 defer,有名返回函数可能会再次修改 var | ||
|
||
真正返回 var 到调用处 | ||
|
||
10 | ||
12 | ||
|
||
10 | ||
6 | ||
|
||
### 分析defer参数的计算时机 | ||
```go | ||
func main() { | ||
a := 1 | ||
b := 2 | ||
defer add("A", a, add("B", a, b)) | ||
a = 0 | ||
defer add("C", a, add("D", a, b)) | ||
b = 1 | ||
} | ||
|
||
|
||
func add(desc string, a, b int) int { | ||
sum := a + b | ||
fmt.Println(desc, a, b, sum) | ||
return sum | ||
} | ||
``` | ||
|
||
defer 语句会计算好 func 的参数,再放入执行栈中。 | ||
B 1 2 3 | ||
D 0 2 2 | ||
C 0 2 2 | ||
A 1 3 4 | ||
|
||
### 考察Go的组合 | ||
```go | ||
type People struct{} | ||
|
||
func (p *People) ShowA() { | ||
fmt.Println("people showA") | ||
p.ShowB() | ||
} | ||
func (p *People) ShowB() { | ||
fmt.Println("people showB") | ||
} | ||
|
||
// Teacher 通过嵌入 People 来获取了 ShowA() 和 showB() | ||
type Teacher struct { | ||
People | ||
} | ||
|
||
// Teacher 实现并覆盖了 showB() | ||
func (t *Teacher) ShowB() { | ||
fmt.Println("teacher showB") | ||
} | ||
|
||
func main() { | ||
t := Teacher{} | ||
t.ShowB() | ||
// 调用未覆盖的 showA(),因为它的 receiver 依旧是 People,相当于 People 调用 | ||
t.ShowA() | ||
} | ||
``` | ||
|
||
```go | ||
|
||
package main | ||
import ( | ||
"fmt" | ||
) | ||
type student struct { | ||
Name string | ||
Age int | ||
} | ||
func pase_student() map[string]*student { | ||
m := make(map[string]*student) | ||
stus := []student{ | ||
{Name: "zhou", Age: 24}, | ||
{Name: "li", Age: 23}, | ||
{Name: "wang", Age: 22}, | ||
} | ||
for _, stu := range stus { | ||
m[stu.Name] = &stu | ||
} | ||
return m | ||
} | ||
func main() { | ||
students := pase_student() | ||
for k, v := range students { | ||
fmt.Printf("key=%s,value=%v \n", k, v) | ||
} | ||
} | ||
``` | ||
|
||
因为for遍历时,变量stu指针不变,每次遍历仅进行struct值拷贝,故m[stu.Name]=&stu实际上一致指向同一个指针,最终该指针的值为遍历的最后一个struct的值拷贝。形同如下代码: | ||
```go | ||
var stu student | ||
for _, stu = range stus { | ||
m[stu.Name] = &stu | ||
} | ||
``` | ||
修正方案,取数组中原始值的指针: | ||
```go | ||
for i, _ := range stus { | ||
stu:=stus[i] | ||
m[stu.Name] = &stu | ||
} | ||
``` | ||
|
||
```go | ||
package main | ||
import ( | ||
"fmt" | ||
) | ||
type People interface { | ||
Speak(string) string | ||
} | ||
type Stduent struct{} | ||
func (stu *Stduent) Speak(think string) (talk string) { | ||
if think == "bitch" { | ||
talk = "You are a good boy" | ||
} else { | ||
talk = "hi" | ||
} | ||
return | ||
} | ||
func main() { | ||
var peo People = Stduent{} | ||
think := "bitch" | ||
fmt.Println(peo.Speak(think)) | ||
} | ||
``` | ||
|
||
编译失败,值类型 Student{} 未实现接口People的方法,不能定义为 People类型。 | ||
|
||
定义为指针 `var peo People = &Stduent{}` | ||
方法定义在值类型上,指针类型本身是包含值类型的方法。 | ||
`func (stu Stduent) Speak(think string) (talk string) { //... }` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
```go | ||
type point struct { | ||
x, y int | ||
} | ||
|
||
func main() { | ||
//Go 为常规 Go 值的格式化设计提供了多种打印方式。例如,这里打印了 point 结构体的一个实例。 | ||
p := point{1, 2} | ||
// %v会自行判断传入参数的格式 | ||
fmt.Printf("%v\n", p) // {1 2} | ||
//如果值是一个结构体,%+v 的格式化输出内容将包括结构体的字段名。 | ||
fmt.Printf("%+v\n", p) // {x:1 y:2} | ||
//%#v 形式则输出这个值的 Go 语法表示。例如,值的运行源代码片段。 | ||
fmt.Printf("%#v\n", p) // main.point{x:1, y:2} | ||
//需要打印值的类型,使用 %T。 | ||
fmt.Printf("%T\n", p) // main.point | ||
//格式化布尔值是简单的。 | ||
fmt.Printf("%t\n", true) | ||
//格式化整形数有多种方式,使用 %d进行标准的十进制格式化。 | ||
fmt.Printf("%d\n", 123) | ||
//这个输出二进制表示形式。 | ||
fmt.Printf("%b\n", 14) | ||
//这个输出给定整数的对应字符。 | ||
fmt.Printf("%c\n", 33) | ||
//%x 提供十六进制编码。 | ||
fmt.Printf("%x\n", 456) | ||
//对于浮点型同样有很多的格式化选项。使用 %f 进行最基本的十进制格式化。 | ||
fmt.Printf("%f\n", 78.9) | ||
//%e 和 %E 将浮点型格式化为(稍微有一点不同的)科学技科学记数法表示形式。 | ||
fmt.Printf("%e\n", 123400000.0) | ||
fmt.Printf("%E\n", 123400000.0) | ||
//使用 %s 进行基本的字符串输出。 | ||
fmt.Printf("%s\n", "\"stringdddddddd\"") | ||
//像 Go 源代码中那样带有双引号的输出,使用 %q。 | ||
fmt.Printf("%q\n", "\"stringccccc\"") | ||
//和上面的整形数一样,%x 输出使用 base-16 编码的字符串,每个字节使用 2 个字符表示。 | ||
fmt.Printf("%x\n", "hex this") | ||
//当输出数字的时候,你将经常想要控制输出结果的宽度和精度,可以使用在 % 后面使用数字来控制输出宽度。默认结果使用右对齐并且通过空格来填充空白部分。 | ||
fmt.Printf("|%6d|%6d|\n", 12, 345) | ||
//你也可以指定浮点型的输出宽度,同时也可以通过 宽度.精度 的语法来指定输出的精度。 | ||
fmt.Printf("|%6.2f|%6.2f|\n", 1.2, 3.45) | ||
//要最对齐,使用 - 标志。 | ||
fmt.Printf("|%-6.2f|%-6.2f|\n", 1.2, 3.45) | ||
//你也许也想控制字符串输出时的宽度,特别是要确保他们在类表格输出时的对齐。这是基本的右对齐宽度表示。 | ||
fmt.Printf("|%6s|%6s|\n", "foo", "b") | ||
//要左对齐,和数字一样,使用 - 标志。 | ||
fmt.Printf("|%-6s|%-6s|\n", "foo", "b") | ||
//到目前为止,我们已经看过 Printf了,它通过 os.Stdout输出格式化的字符串。Sprintf 则格式化并返回一个字符串而不带任何输出。 | ||
s := fmt.Sprintf("a %s", "string") | ||
fmt.Println(s) | ||
//你可以使用 Fprintf 来格式化并输出到 io.Writers而不是 os.Stdout。 | ||
fmt.Fprintf(os.Stderr, "an %s\n", "error") | ||
|
||
fmt.Println(fmt.Sprintf("\x1b[%dm%s\x1b[0m", 3, "hello")) | ||
} | ||
``` | ||
|
||
```go | ||
type Data struct { | ||
} | ||
|
||
func (self Data) String() string { | ||
return "data" | ||
} | ||
|
||
func main() { | ||
fmt.Printf("hello world\n") | ||
|
||
fmt.Println("hello world") | ||
|
||
fmt.Printf("num %d\n", 5) | ||
|
||
str := fmt.Sprintf("float %f", 3.14159) | ||
fmt.Print(str) | ||
|
||
fmt.Fprintln(os.Stdout, "A\n") | ||
|
||
// 自动转变格式, 调用接口体中的String()方法 | ||
fmt.Printf("%v\n", Data{}) | ||
|
||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.