- Go Machine Learning Projects
- Xuanyi Chew
- 355字
- 2021-06-10 18:46:32
Functions
Functions are the main way that anything is computed in Go.
This is a function:
func addInt(a, b int) int { return a + b }
We call func addInt(a, b int) int, which is the function signature. The function signature is composed of the function name, parameters, and return type(s).
The name of the function is addInt. Note the formatting being used. The function name is in camelCase—this is the preferred casing of names in Go. The first letter of any name, when capitalized, like AddInt indicates that it should be exported. By and large in this book we shan't worry about exported or unexported names, as we will be mostly using functions. But if you are writing a package, then it matters. Exported names are available from outside a package.
Next, note that a and b are parameters, and both have the type int. We'll discuss types in a bit, but the same function can also be written as:
func addInt(a int, b int) int { return a + b }
Following that, this is what the function returns. This function addInt returns an int. This means when a function is called correctly, like so:
z := addInt(1, 2)
z will have a type int.
After the return type is defined, {...} denotes the body. When {...} is written in this book, it means the content of the function body is not as important for the discussion at hand. Some parts of the book may have snippets of function bodies, but without the signature func foo(...). Again those snippets are the snippets under discussion. It's expected that the reader will piece together the function from context in the book.
A Go function may return multiple results. The function signature looks something like this:
func divUint(a, b uint) (uint, error) { ... }
func divUint(a, b uint) (retVal uint, err error) { ... }
Again, the difference is mainly in naming the return values. In the second example, the return values are named retVal and err respectively. retVal is of type uint and err is of type error.
- 計算機控制技術(shù)
- Cloud Analytics with Microsoft Azure
- iClone 4.31 3D Animation Beginner's Guide
- B2B2C網(wǎng)上商城開發(fā)指南
- STM32嵌入式微控制器快速上手
- Microsoft System Center Confi guration Manager
- SQL Server數(shù)據(jù)庫應(yīng)用基礎(chǔ)(第2版)
- 網(wǎng)絡(luò)服務(wù)器搭建與管理
- Practical Network Automation
- Oracle 11g基礎(chǔ)與提高
- Kubernetes Design Patterns and Extensions
- TensorFlow 2.0卷積神經(jīng)網(wǎng)絡(luò)實戰(zhàn)
- Proteus從入門到精通100例
- 仿狗機器人的設(shè)計與制作
- 玩中學:樂高機器人入門(上冊) (中小學創(chuàng)客教育執(zhí)委會推薦教材)