- Go Machine Learning Projects
- Xuanyi Chew
- 217字
- 2021-06-10 18:46:32
Methods
Let's say we wrote these functions, and we have defined email as before:
type email string
func check(a email) { ... }
func send(a email, msg string) { ... }
Observe that email is always the first type in the function parameters.
Calling the functions look something like this:
e := "john@smith.com"
check(e)
send(e, "Hello World")
We may want to make that into a method of the email type. We can do so as follows:
type email string
func (e email) check() { ... }
func (e email) send(msg string) { ... }
(e email) is called the receiver of the method.
Having defined the methods thus, we may then proceed to call them:
e := "john@smith.com"
e.check()
e.send("Hello World")
Observe the difference between the functions and methods. check(e) becomes e.check(). send(e, "Hello World") becomes e.send("Hello World"). What's the difference other than syntactic difference? The answer is, not much.
A method in Go is exactly the same as a function in Go, with the receiver of the method as the first parameter of the function. It is unlike methods of classes in object-oriented programming languages.
So why bother with methods? For one, it solves the expression problem quite neatly. To see how, we'll look at the feature of Go that ties everything together nicely: interfaces.
- AWS:Security Best Practices on AWS
- 網(wǎng)頁編程技術(shù)
- 影視后期制作(Avid Media Composer 5.0)
- 群體智能與數(shù)據(jù)挖掘
- 21天學(xué)通C++
- Creo Parametric 1.0中文版從入門到精通
- 永磁同步電動機(jī)變頻調(diào)速系統(tǒng)及其控制(第2版)
- 大學(xué)計(jì)算機(jī)應(yīng)用基礎(chǔ)
- 基于32位ColdFire構(gòu)建嵌入式系統(tǒng)
- Machine Learning with Apache Spark Quick Start Guide
- Spark大數(shù)據(jù)商業(yè)實(shí)戰(zhàn)三部曲:內(nèi)核解密|商業(yè)案例|性能調(diào)優(yōu)
- ADuC系列ARM器件應(yīng)用技術(shù)
- Java求職寶典
- 工廠電氣控制設(shè)備
- 歐姆龍PLC應(yīng)用系統(tǒng)設(shè)計(jì)實(shí)例精解