- 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.
- IoT Penetration Testing Cookbook
- 計算機控制技術
- 80x86/Pentium微型計算機原理及應用
- 21天學通Java
- 計算機網絡安全
- Machine Learning with Apache Spark Quick Start Guide
- LAMP網站開發黃金組合Linux+Apache+MySQL+PHP
- Working with Linux:Quick Hacks for the Command Line
- Unity Multiplayer Games
- 簡明學中文版Photoshop
- 機器人剛柔耦合動力學
- Linux常用命令簡明手冊
- ARM嵌入式系統開發完全入門與主流實踐
- 數據庫基礎:Access
- NetSuite ERP for Administrators