0%

Golang中的方法

Golang并非是面向对象的语言,但是它可以模拟面向对象。Golang中的struct就类似于面向对象语言中的类,那么既然有了类,就要有对应的类的方法。Golang中以接收器(reciever)的形式实现struct的方法。

1.方法的声明

方法声明的形式如下

1
2
3
4
type mytype struct {}

func (m mytype) method(para) return_type {}
func (m *mytype) method(para) return_type {}
Read more »

Logstash to Logstash Communication

实现logstash与logstash之间的通信需要通过lumberjack插件实现。

1.生成证书

执行openssl req -x509 -days 3560 -batch -nodes -newkey rsa:2048 -keyout lumberjack.key -out lumberjack.cert -subj /CN=localhost

Read more »

回调函数和闭包

高阶函数

golang中的高阶函数有以下特性:

  • 函数可以作为另一个函数的参数(常用于回调函数)
  • 函数可以作为另一个函数的返回值(常用于闭包)
  • 函数可以被赋值给一个变量

1.函数作为参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import "fmt"

func added(msg string, a func(a, b int) int) {
fmt.Println(msg, ":", a(33, 44))
}

func main() {
// 函数内部不能嵌套命名函数
// 所以main()中只能定义匿名函数
f := func(a, b int) int {
return a + b
}
added("a+b", f)
}
Read more »