Go by Example: Functions

函数(Function)是Go的核心,我们将会通过多个不同的例子来学习函数。

package main

import "fmt"

    // 这个函数输入两个int型数据,计算并返回int型的和
func plus(a int, b int) int {
    // Go需要使用return语句显式地返回值
    // 它不会自动返回函数中最后一个表达式的值。
    return a + b
}

func main() {
    // 正如你所想的那样,通过“name(args)”来调用函数
    res := plus(1, 2)
    fmt.Println("1+2 =", res)
}

输出

$ go run functions.go
1+2 = 3

Go语言的函数还有其他一些功能。其中一个就是下一章节将会学习的多值返回。

下一个例子: Go by Example: Multiple Return Values。

英文原文

时间: 2024-10-10 05:20:14

Go by Example: Functions的相关文章

Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)

catalog 1. How to Add New Functions to MySQL 2. Features of the User-Defined Function Interface 3. User-Defined Function 4. UDF Argument Processing 5. UDF Return Values and Error Handling 6. UDF Compiling and Installing 7. Adding a New Native Functio

POJ 1080 Human Gene Functions(LCS)

Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four letters, A, C, G, and T. Biologists have been interested in identifying human genes and determining their

学习Oracle分析函数(Analytic Functions)

Oracle提供了一些功能很强大的分析函数,使用这些函数可以完成可能需要存储过程来实现的需求. 分析函数计算基于一组数据行的聚合值,它们不同于聚合函数的是,它们为每一组返回多行结果.分析函数是除ORDER BY子句之外,在查询语句中最后执行的.所有的join和所有的WHERE ,GROUP BY 和HAVING子句都在分析函数之前执行.所以分析函数只能出现在select或ORDER BY子句中. 下图为11.2版本官方文档中给出的语法示意图: 下面简单介绍一下各个部分: analytic_fun

模板类的约束模板友元函数:template friend functions

本来这篇博客是不打算写的,内容不是很难,对于我自己来讲,更多的是为了突出细节. 所谓template friend functions,就是使友元函数本身成为模板.基本步骤:1,在类定义的前面声明每个模板函数.eg:template <typename T> void counts(); template <typename T> void report<>(T &);2,在类声明中再次将模板声明为友元. template <typename TT>

&lt;QtEndian&gt; - Endian Conversion Functions

The <QtEndian> header provides functions to convert between little and big endian representations of numbers. More... Functions T qFromBigEndian(const uchar * src)T qFromBigEndian(T src)T qFromLittleEndian(const uchar * src)T qFromLittleEndian(T src

python3.4 build in functions from 官方文档 翻译中

2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.     Built-in Funct

The difference of Methods and Functions

Functions Functions are self-contained chunks of code that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed. Swift’s unified function syntax

NVIDIA CG语言 函数之所有数学类函数(Mathematical Functions)

数学类函数(Mathematical Functions) abs(x) 返回标量和向量x的绝对值 如果x是向量,则返回每一个成员的绝对值 acos(x) 返回标量和向量x的反余弦 x的范围是[-1,1],返回值的范围是[0,π], 如果x是向量,则返回每一个成员的反余弦 all(x) 如果一个布尔标量为真,或者布尔向量的所有成员为真,则返回真 any(x) 如果一个布尔标量为真,或者布尔向量成员存在真值,则返回真 asin(x) 返回标量和向量x的反正弦 x的范围是[-1,1],返回值的范围是

Functions

1 Local static variables Local static variables are not destroyed when the function ends; they are destroyed when the program terminates. 2. Function parameter list Parameter names are optional. However, there is no way to use an unnamed parameter. A

Execution Order of Event Functions

In Unity scripting, there are a number of event functions that get executed in a predetermined order as a script executes. This execution order is described below: Editor Reset: Reset is called to initialize the script’s properties when it is first a