mirror of
https://gitee.com/shikong-sk/golang-study
synced 2025-02-23 15:32:15 +08:00
docs: 函数类型 参数、返回值
This commit is contained in:
parent
da6f7409b0
commit
90c1c5c344
52
base/function/type/main.go
Normal file
52
base/function/type/main.go
Normal file
@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func f1() {
|
||||
fmt.Println("exec f1()")
|
||||
}
|
||||
|
||||
func f2() string {
|
||||
return fmt.Sprintln("exec f2()")
|
||||
}
|
||||
|
||||
// 函数 也可作为 参数 的类型
|
||||
func f3(f func() string) {
|
||||
fmt.Println("exec f3()")
|
||||
fmt.Printf(f())
|
||||
}
|
||||
|
||||
// 函数 还可以作为 返回值 类型
|
||||
func f4(x, y int) func() string {
|
||||
return func() string {
|
||||
return "inner func: " + strconv.Itoa(x+y) + "\n"
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 函数类型 参数
|
||||
mf1 := f1
|
||||
mf1()
|
||||
fmt.Printf("mf1: %T\n", mf1)
|
||||
|
||||
fmt.Println("=========================================================")
|
||||
|
||||
mf2 := f2
|
||||
fmt.Printf(mf2())
|
||||
fmt.Printf("mf2: %T\n", mf2)
|
||||
|
||||
fmt.Println("=========================================================")
|
||||
|
||||
mf3 := f3
|
||||
mf3(mf2)
|
||||
fmt.Printf("mf3: %T\n", mf3)
|
||||
|
||||
fmt.Println("=========================================================")
|
||||
|
||||
mf3(f4(2, 2))
|
||||
fmt.Printf("f4: %T\n", f4)
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user