Tag: return name is attribute www. Expression Method Parameter result
Go Language Learning Note seven: functions
There are functions and methods in the Go language, magic does not. It's kind of like python.
function definition
func function_name( [parameter list] ) [return_types] { 函数体}
As an example:
/* 函数返回两个数的最大值 */func max(num1, num2 int) int { /* 声明局部变量 */ var result int if (num1 > num2) { result = num1 } else { result = num2 } return result }
Function call
var a int = 1var b int = 2var c intc = max(a, b)
function can return multiple values
func swap(x, y string) (string, string) { return y, x}func main() { a, b := swap("hello", "kris") fmt.Println(a, b)}
is not a bit like python.
Value passing
Passing means that a copy of the actual parameter is passed to the function when the function is called, so that if the parameters are modified in the function, the actual parameters will not be affected.
The underlying type is a value pass, as the Earth people know.
Reference delivery
Reference passing refers to the passing of the address of the actual parameter to the function when the function is called, and the modification of the parameter in the function will affect the actual parameter.
func swap(x *int, y *int) { var temp int temp = *x /* 保持 x 地址上的值 */ *x = *y /* 将 y 值赋给 x */ *y = temp /* 将 temp 值赋给 y */}
God, I feel like I'm back in school. (Feel good Young)
function as a value
The Go language makes it possible to create functions flexibly and use them as values.
func main(){ /* 声明函数变量 */ getSquareRoot := func(x float64) float64 { return math.Sqrt(x) } /* 使用函数 */ fmt.Println(getSquareRoot(9))}
Does it feel like a JavaScript language.
Closed Package
The Go language supports anonymous functions, which can be used as closures. An anonymous function is an "inline" statement or an expression. The advantage of anonymous functions is that variables within functions can be used directly, without stating.
func getSequence() func() int { i := 0 return func() int { i += 1 return i }}func main() { next := getSequence() fmt.Println(next()) fmt.Println(next())}
This writing is too ... Perfect (true fashion). I guess func() int it's the return value of the closure.
Method
There are both functions and methods in the Go language. A method is a function that contains the recipient, which can be either a value of a named type or struct type or a pointer. All methods of a given type belong to the method set of that type.
func (variable_name variable_data_type) function_name() [return_type]{ /* 函数体*/}
As an example:
/* 定义结构体 */type Circle struct { radius float64}func main() { var c1 Circle c1.radius = 10.00 fmt.Println("Area of Circle(c1) = ", c1.getArea())}//该 method 属于 Circle 类型对象中的方法func (c Circle) getArea() float64 { //c.radius 即为 Circle 类型对象中的属性 return 3.14 * c.radius * c.radius}
The feeling is awkward, Circle gives me the feeling of a Java class, and Getarea () is the method of this class. It is estimated that as long as the function (c Circle) is preceded by a method that belongs to C.
Other articles in this series address
Https://github.com/zhangqunshi/golang_study
Reference: http://www.runoob.com/go/go-method.html
Go Language Learning Note seven: functions