func SquaresOfSumAndDiff(a int64, b int64) (s int64, d int64) {
x, y := a + b, a - b
s = x * x
d = y * y
return // <=> return s, d
}
func
keyword.
SquareOfSumAndDiff
.
()
.
()
. However, if the list is blank or it is composed of only one anonymous result declaration, then the pair of ()
in result declaration list is optional (see below for details).
{}
. In a function body, the return
keyword is used to end the normal forward execution flow and enter the exiting phase (see the section after next) of a call of the function.
var
keywords. The above declared function has two parameters, a
and b
, and has two results, s
and d
. All the types of the parameters and results are int64
. Parameters and results are treated as local variables within their corresponding function bodies.
return
keyword must be followed by a sequence of return values, each of them corresponds to a result declaration of the function declaration. For example, the following function declaration is equivalent to the above one.
func SquaresOfSumAndDiff(a int64, b int64) (int64, int64) {
return (a+b) * (a+b), (a-b) * (a-b)
}
0 false
.
func f() (x int, y bool) {
println(x, y) // 0 false
return
}
SquaresOfSumAndDiff
are equivalent to
func SquaresOfSumAndDiff(a, b int64) (s, d int64) {
return (a+b) * (a+b), (a-b) * (a-b)
// The above line is equivalent
// to the following line.
/*
s = (a+b) * (a+b); d = (a-b) * (a-b); return
*/
}
return
keyword can be followed with return values.
()
. If the function declaration has no return results, then the result declaration list portion can be omitted totally. The parameter declaration list portion can never be omitted, even if the number of parameters of the declared function is zero.
func CompareLower4bits(m, n uint32) (r bool) {
r = m&0xF > n&0xF
return
// The above two lines is equivalent to
// the following line.
/*
return m&0xF > n&0xF
*/
}
// This function has no parameters. The result
// declaration list is composed of only one
// anonymous result declaration, so it is not
// required to be enclosed within ().
func VersionString() string {
return "go1.0"
}
// This function has no results. And all of
// its parameters are anonymous, for we don't
// care about them. Its result declaration
// list is blank (so omitted).
func doNothing(string, int) {
}
main
entry function in each Go program is declared without parameters and results.
()
. We often call this as argument passing (or parameter passing). Each single-value argument corresponds to (is passed to) a parameter.
package main
func SquaresOfSumAndDiff(a int64, b int64) (int64, int64) {
return (a+b) * (a+b), (a-b) * (a-b)
}
func CompareLower4bits(m, n uint32) (r bool) {
r = m&0xF > n&0xF
return
}
// Initialize a package-level variable
// with a function call.
var v = VersionString()
func main() {
println(v) // v1.0
x, y := SquaresOfSumAndDiff(3, 6)
println(x, y) // 81 9
b := CompareLower4bits(uint32(x), uint32(y))
println(b) // false
// "Go" is deduced as a string,
// and 1 is deduced as an int32.
doNothing("Go", 1)
}
func VersionString() string {
return "v1.0"
}
func doNothing(string, int32) {
}
package main
func main() {
// This anonymous function has no parameters
// but has two results.
x, y := func() (int, int) {
println("This function has no parameters.")
return 3, 4
}() // Call it. No arguments are needed.
// The following anonymous function have no results.
func(a, b int) {
// The following line prints: a*a + b*b = 25
println("a*a + b*b =", a*a + b*b)
}(x, y) // pass argument x and y to parameter a and b.
func(x int) {
// The parameter x shadows the outer x.
// The following line prints: x*x + y*y = 32
println("x*x + y*y =", x*x + y*y)
}(y) // pass argument y to parameter x.
func() {
// The following line prints: x*x + y*y = 25
println("x*x + y*y =", x*x + y*y)
}() // no arguments are needed.
}
x
and y
variables declared above, it can use the two variables directly. Such functions are called closures. In fact, all custom functions in Go can be viewed as closures. This is why Go functions are as flexible as many dynamic languages.
println
and print
functions. We can call these functions without importing any packages.
real
and imag
functions to get the real and imaginary parts of a complex value. We can use the built-in complex
function to produce a complex value. Please note, if any of the arguments of a call to any of the two functions are all constants, then the call will be evaluated at compile time, and the result value of the call is also a constant. In particular, if any of the arguments is an untyped constant, then the result value is also an untyped constant. The call is viewed as a constant expression.
// c is a untyped complex constant.
const c = complex(1.6, 3.3)
// The results of real(c) and imag(c) are both
// untyped floating-point values. They are both
// deduced as values of type float32 below.
var a, b float32 = real(c), imag(c)
// d is deduced as a typed value of type complex64.
// The results of real(d) and imag(d) are both
// typed values of type float32.
var d = complex(a, b)
// e is deduced as a typed value of type complex128.
// The results of real(e) and imag(e) are both
// typed values of type float64.
var e = c
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.
reflect
standard package.sync
standard package.sync/atomic
standard package.