Functions----Go的成员之一函数

153 阅读1分钟

原文

A function can take zero or more arguments.
In this example, add takes two parameters of type int.
Notice that the type comes after the variable name.
(For more about why types look the way they do, see the article on Go's declaration syntax.)

代码

package main

import "fmt"

func add(x int, y int) int {
	return x + y
}

func main() {
	fmt.Println(add(42, 13))
}

翻译

一个函数可以携带0个或者多个参数
在这个例子里面,我们携带了两个int参数
注意这个类型是放置在变量名的后面

总结

说起来也奇怪啊,一般的语言,Java c cpp python js等等这种语言,一般的函数写法都是

返回值 function 函数名(类型 变量_1,类型 变量_2,....){
    .............具体的内容
}

这个go倒好,完全给你反过来了

func 函数名(变量_1 类型,变量_2 类型,.....) 返回值{
    ............具体的内容
}

补充

这篇文章是作者说明,我们Go为啥子这么定义方法的写法形式,就是刚,不落俗套