Comments (6)
能否讲一下这个函数类型的意义,不然为什么不直接调用想调用的函数,而要用函数类型去调用呢?这个在python和C++中都没有这种用法,为什么go要有这种用法呢?
from golangcodingtime.
能否讲一下这个函数类型的意义,不然为什么不直接调用想调用的函数,而要用函数类型去调用呢?这个在python和C++中都没有这种用法,为什么go要有这种用法呢?
函数在定义时,入参和返回,都要定好其类型。
常见的入参和返回,是对象。
但也有一些入参和返回,是函数,比如回调函数。
用这个来解释,不知道你好理解不?
from golangcodingtime.
能否讲一下这个函数类型的意义,不然为什么不直接调用想调用的函数,而要用函数类型去调用呢?这个在python和C++中都没有这种用法,为什么go要有这种用法呢?
问题是:函数本身可不可以被看成对象。
看起来是可以的,函数本身就是对象,通过函数与对象绑定形成方法,这样可以实现函数类型调用某个方法。
from golangcodingtime.
能否讲一下这个函数类型的意义,不然为什么不直接调用想调用的函数,而要用函数类型去调用呢?这个在python和C++中都没有这种用法,为什么go要有这种用法呢?
设计思维:函数可以调用函数,那么在java中都是直接this.anotherMethod(int someParams);
但是到了go里面,由于设计语言的 人员不一样,这些人似乎吸收了前端语言和其他语言的优势,然后可以设置函数做为参数传入,作为返回值返回函数,这本身想就是可以的。只是之前没有这样设计而已,函数作为参数传入,就可以在某些条件下去回调这个函数,比如说:
var(
status = 0; //0 代表饿;1代表吃饱
)
type check func(n int)
func (c check) eatMeal(food string) {
if(status ==1) {
fmt.Println(c(food))
}
fmt.Println("我正在吃:", food)
}
func sayNo(){
fmt.Println("我吃饱了")
}
func main() {
m := check(sayNo)
status = 1
m.eatMeal("苗条")
}
from golangcodingtime.
参考java中 lambda表达式**
from golangcodingtime.
好处是可以对一个方法进行装饰。这里定义一个函数对象可以将符合该模式的函数重新包装,起到 aop 的作用
from golangcodingtime.
Related Issues (20)
- aboutme HOT 3
- c07/c07_03 HOT 1
- c01/c01_06 HOT 3
- c01/c01_10 HOT 1
- c01/c01_07 HOT 1
- c01/c01_09 HOT 1
- c01/c01_13 HOT 1
- c01/c01_15 HOT 1
- c02/c02_01 HOT 2
- c04/c04_06 HOT 3
- c05/c05_02 HOT 2
- chapters/p02 HOT 2
- index HOT 1
- c02/c02_03 HOT 1
- c02/c02_04 HOT 1
- c02/c02_05 HOT 1
- c04/c04_01 HOT 1
- c04/c04_03 HOT 1
- c07/c07_04 HOT 1
- c01/c01_03 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from golangcodingtime.