当前位置: 移动技术网 > IT编程>脚本编程>Go语言 > [Go] golang结构体成员与函数类型

[Go] golang结构体成员与函数类型

2019年02月21日  | 移动技术网IT编程  | 我要评论
package main

import (
	"fmt"
)

//定义一个类型
type tsh struct {
	//定义成员,类型是func() string
	test func() string
}

//定义一个函数,获取tsh类型
func new(fn func() string) *tsh {
	return &tsh{
		test: fn,
	}
}
func cre() string {
	return fmt.sprintf("%s,来了", "tsh")
}

func main() {
	//new完得到tsh类型,调用该结构体的test成员,该成员是个函数
	res := new(cre).test()
	fmt.println(res)
}

某些类的逻辑非常复杂,简化代码帮助理解下逻辑  

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网