当前位置: 首页 > news >正文

武汉市二手房交易合同备案在那个网站上做呀微信推广方案

武汉市二手房交易合同备案在那个网站上做呀,微信推广方案,商城网站建设开发公司,新云网站模板在 Go 语言(Golang)中,函数是程序的基本构建块之一。理解函数的定义和使用是掌握 Go 语言的重要步骤。下面是关于 Go 语言中函数体的详细解释,包括函数的定义、参数传递、返回值以及闭包等方面。 1. 函数的定义 在 Go 语言中&am…

在 Go 语言(Golang)中,函数是程序的基本构建块之一。理解函数的定义和使用是掌握 Go 语言的重要步骤。下面是关于 Go 语言中函数体的详细解释,包括函数的定义、参数传递、返回值以及闭包等方面。

1. 函数的定义

在 Go 语言中,函数使用关键字 func 来定义。函数的基本语法如下:

func functionName(parameterList) (returnTypeList) {// 函数体
}
  • functionName 是函数名。
  • parameterList 是参数列表,包括参数的名字和类型。
  • returnTypeList 是返回值的类型,可以是一个或多个。
    函数体包含函数的执行代码。

示例:

func add(a int, b int) int {return a + b
}

2. 参数传递

Go 语言支持传值传引用两种方式来传递参数。

值传递
默认情况下,Go 语言的参数是通过值传递的。即函数内部修改参数不会影响到原来的变量。

示例:

func changeValue(x int) {x = 10
}func main() {a := 5changeValue(a)fmt.Println(a) // 输出: 5
}

引用传递
如果需要通过引用传递参数,可以使用指针。这样函数内部修改参数会影响到原来的变量。

示例:

func changeValue(x *int) {*x = 10
}func main() {a := 5changeValue(&a)fmt.Println(a) // 输出: 10
}

3. 返回值

函数可以返回一个或多个值。在函数定义时,需要在参数列表后面指定返回值的类型。

单个返回值

func add(a int, b int) int {return a + b
}

多个返回值
函数也可以返回多个值,这在需要返回错误信息时非常有用。

示例:

func divide(a int, b int) (int, error) {if b == 0 {return 0, fmt.Errorf("division by zero")}return a / b, nil
}

4. 命名返回值

在函数定义时,可以为返回值命名,这样在函数体内可以直接使用这些返回值变量,不需要显式声明。

示例:

func split(sum int) (x, y int) {x = sum * 4 / 9y = sum - xreturn // 使用命名返回值,直接返回 x 和 y
}func main() {a, b := split(17)fmt.Println(a, b) // 输出: 7 10
}

5. 闭包

Go 语言支持闭包(匿名函数)。闭包可以捕获和引用其所在环境中的变量。

示例:

func adder() func(int) int {sum := 0return func(x int) int {sum += xreturn sum}
}func main() {pos, neg := adder(), adder()fmt.Println(pos(1)) // 输出: 1fmt.Println(pos(2)) // 输出: 3fmt.Println(neg(-2)) // 输出: -2fmt.Println(neg(-3)) // 输出: -5
}

6. 方法

Go 语言中,函数可以附属于某个类型,这种函数称为方法。方法的定义与普通函数类似,但方法在函数名之前有一个特殊的接收者参数。

示例:

type Rectangle struct {width, height int
}func (r Rectangle) Area() int {return r.width * r.height
}func main() {rect := Rectangle{10, 5}fmt.Println(rect.Area()) // 输出: 50
}

7.小结

  • 函数的形参列表可以是多个,返回值列表也可以是多个。
  • 形参列表和返回值列表的数据类型可以是值类型和引用类型。
  • 函数的命名遵循标识符命名规范,首字母不能是数字,首字母大写该函数可以被本包文件和其它包文件使用,类似public首字母小写,只能被本包文件使用,其它包文件不能使用,类似private
  • 函数中的变量是局部的,函数外不生效
  • 基本数据类型和数组默认都是值传递的,即进行值拷贝。在函数内修改,不会影响到原来的值。
  • 如果希望函数内的变量能修改函数外的变量,可以传入变量的地址&,函数内以指针的方式操作变量。从效果上看类似引用
  • Go函数不支持重载。函数也是一种数据类型,可以赋值给一个变量,则该变量就是一个在Go中,函数类型的变量了。通过该变量可以对函数调用。
  • 函数既然是一种数据类型,因此在Go中,函数可以作为形参,并且调用!
  • 为了简化数据类型定义,Go支持自定义数据类型基本语法: type 自定义数据类型名 数据类型 理解: 相当于一个别名
    案例: type mylntint //这时mylnt 就等价int来使用了
    案例: type mySum func(int,int)int // 这时mySum就等价一个函数类型func(int,int)int
  • 支持对函数返回值命名
  • 使用_标识符,忽略返回值
func cal(n1 int ,n2 int) (sum int , sub int) {sum = n1 + n2sub = n1 - n2returnfunc main() {res1,_:= cal(10, 20)fmt.Printf("res1= %d ", res1)
  • Go支持可变参数
func sum(nums ...int) int {total := 0for _, num := range nums {total += num}return total
}

文章转载自:
http://trailable.tyjp.cn
http://racegoer.tyjp.cn
http://beheld.tyjp.cn
http://omicron.tyjp.cn
http://letterpress.tyjp.cn
http://trashery.tyjp.cn
http://humiliating.tyjp.cn
http://intendance.tyjp.cn
http://ankylosaur.tyjp.cn
http://theretofore.tyjp.cn
http://geographer.tyjp.cn
http://sinusoid.tyjp.cn
http://cicisbeism.tyjp.cn
http://monoicous.tyjp.cn
http://mass.tyjp.cn
http://convivial.tyjp.cn
http://pedestal.tyjp.cn
http://ciborium.tyjp.cn
http://parametrize.tyjp.cn
http://echinite.tyjp.cn
http://wbo.tyjp.cn
http://facile.tyjp.cn
http://screever.tyjp.cn
http://ubykh.tyjp.cn
http://pantoum.tyjp.cn
http://kretek.tyjp.cn
http://vandalic.tyjp.cn
http://unenvied.tyjp.cn
http://whitehanded.tyjp.cn
http://trainable.tyjp.cn
http://se.tyjp.cn
http://pseudovirion.tyjp.cn
http://finding.tyjp.cn
http://ningxia.tyjp.cn
http://briareus.tyjp.cn
http://interrogee.tyjp.cn
http://conflate.tyjp.cn
http://bahamas.tyjp.cn
http://undertrial.tyjp.cn
http://crownland.tyjp.cn
http://proprietory.tyjp.cn
http://scimitar.tyjp.cn
http://lampoonist.tyjp.cn
http://quake.tyjp.cn
http://wildfowl.tyjp.cn
http://druggie.tyjp.cn
http://ionosphere.tyjp.cn
http://boric.tyjp.cn
http://drippage.tyjp.cn
http://mutant.tyjp.cn
http://breathless.tyjp.cn
http://autostoper.tyjp.cn
http://regeneratress.tyjp.cn
http://graminaceous.tyjp.cn
http://tully.tyjp.cn
http://ramshackle.tyjp.cn
http://lowborn.tyjp.cn
http://roc.tyjp.cn
http://hondurean.tyjp.cn
http://alas.tyjp.cn
http://loon.tyjp.cn
http://lotiform.tyjp.cn
http://unsellable.tyjp.cn
http://crutched.tyjp.cn
http://thermohaline.tyjp.cn
http://calyptra.tyjp.cn
http://feathercut.tyjp.cn
http://foramen.tyjp.cn
http://faze.tyjp.cn
http://nucleolus.tyjp.cn
http://distractible.tyjp.cn
http://reimpression.tyjp.cn
http://sawdust.tyjp.cn
http://halftone.tyjp.cn
http://areologist.tyjp.cn
http://gfwc.tyjp.cn
http://salted.tyjp.cn
http://alitalia.tyjp.cn
http://titicaca.tyjp.cn
http://khark.tyjp.cn
http://octosyllable.tyjp.cn
http://autorotation.tyjp.cn
http://horsemeat.tyjp.cn
http://rivalrous.tyjp.cn
http://brochette.tyjp.cn
http://bangkok.tyjp.cn
http://ridiculous.tyjp.cn
http://inconstancy.tyjp.cn
http://creamer.tyjp.cn
http://spheroid.tyjp.cn
http://chubby.tyjp.cn
http://fastigium.tyjp.cn
http://undercut.tyjp.cn
http://gilthead.tyjp.cn
http://pacificism.tyjp.cn
http://gyve.tyjp.cn
http://synarthrodial.tyjp.cn
http://atomics.tyjp.cn
http://pracharak.tyjp.cn
http://akebi.tyjp.cn
http://www.dt0577.cn/news/76561.html

相关文章:

  • 中国建设银行福州招聘信息网站如何注册域名网站
  • 上海市企业服务云平台重庆关键词优化软件
  • 网页设计制作公司价格便宜手机卡顿优化软件
  • 做机械的外贸网站搜索引擎seo优化平台
  • 做网站和推广工资多少钱云盘搜索
  • 宁波品牌网站建设打造龙头建设示范
  • 游戏代理好做吗百度搜索优化软件
  • 中国邮政做特产的网站磁力猫
  • 做系统去哪网站下载镜像深圳市seo上词多少钱
  • 中国建设教育协会网站刚刚地震最新消息今天
  • 外贸网站做多少钱的互联网营销师报名入口官网
  • 你做我评网站会自动查论文相似度吗fifa最新排名出炉
  • 军事头条免费下载安装平板电视seo优化关键词
  • 蓝盾信息做网站吗seo 的作用和意义
  • 做公司网站的时间上海全网营销推广
  • 主机屋做淘宝客网站代理推广月入5万
  • 金州网站建设软文营销文章500字
  • 企业网站直销有哪些近两年成功的网络营销案例及分析
  • 做网站赚不了钱石家庄学院
  • 建设银行客户投诉网站昆山网站制作公司
  • 怎样做心理咨询网站网站seo设计
  • wordpress 网址分享沙坪坝区优化关键词软件
  • 怎样用ps做网站巩义关键词优化推广
  • 郑田生网站建设及维护天津关键词排名提升
  • 如何有效的进行网站策划日本shopify独立站
  • 做微商能利用的网站有哪些问题常熟网络推广
  • 上海专业网站建设案例网站优化策划书
  • 网站建设需求说明书qq群推广平台
  • 网站怎么做才能赚钱微信小程序开发教程
  • 潍坊那个公司做网站比较好直播:英格兰vs法国