Golang中的函数类型

Golang中的函数类型Demo

package test

import (
	"fmt"
)

//函数类型作为返回值
func GetFunc() func() {
	return func() {
		fmt.Println("函数类型作为返回值")
	}
}

//函数类型作为参数
func PrintType(f func()) {
	fmt.Printf("f type : %T", f)
	f()
}

主函数测试

package main

import (
	"test1/test"
)

/*
 * @Author: zhaojl
 * @Date: 2022-06-11 15:58:48
 * @LastEditTime: 2022-06-12 22:47:57
 * @LastEditors: zhaojl
 * @Description:
 * @FilePath: \test1\main.go
 */

func main() {

	//函数类型作为返回值
	f := test.GetFunc()
	f()

	//函数类型作为参数

	test.PrintType(func() {
	})
}

版权声明:本文为weixin_38334306原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。