-
Notifications
You must be signed in to change notification settings - Fork 153
/
func_advance.go
41 lines (32 loc) · 861 Bytes
/
func_advance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import "fmt"
import "math"
// define function getSquareRoot1
func getSquareRoot1(x float64) float64 {
return math.Sqrt(x)
}
// deffine a function variable
var getSquareRoot2 = func(x float64) float64 {
return math.Sqrt(x)
}
// define a function type
type callback_func func(int) int
// function calValue accepts a function variable cb as its second argument
func calValue(x int, cb callback_func) int{
fmt.Println("[func|calValue]")
return cb(x)
}
func realFunc(x int) int {
fmt.Println("[func|realFunc]callback function")
return x*x
}
func main() {
num := 100.00
result1 := getSquareRoot1(num)
result2 := getSquareRoot2(num)
fmt.Println("result1=", result1)
fmt.Println("result2=", result2)
value := 81
result3 := calValue(value, realFunc) // use function realFunc as argument of calValue
fmt.Println("result3=", result3)
}