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

怎么在网站上做宣传全网软文推广

怎么在网站上做宣传,全网软文推广,java开发就是做网站么,wordpress 删除自定义栏目函数是⼀等公⺠、学习函数式编程、可变参数及 defer - GO语言从入门到实战 函数是⼀等公⺠ 在Go语言中,函数可以分配给一个变量,可以作为函数的参数,也可以作为函数的返回值。这样的行为就可以理解为函数属于一等公民。 与其他主要编程语⾔…

函数是⼀等公⺠、学习函数式编程、可变参数及 defer - GO语言从入门到实战

函数是⼀等公⺠、学习函数式编程、可变参数及 defer

函数是⼀等公⺠

在Go语言中,函数可以分配给一个变量,可以作为函数的参数,也可以作为函数的返回值。这样的行为就可以理解为函数属于一等公民。

与其他主要编程语⾔的差异

  1. 可以有多个返回值:与其他一些编程语言不同,Go语言中的函数可以返回多个值。这使得函数可以更有效地处理和传递多个结果。例如,你可以使用两个变量来接收函数的两个返回值。
package fn_testimport ("fmt"
)
func divideAndRemainder(dividend, divisor int) (int, int) {quotient := dividend / divisorremainder := dividend % divisorreturn quotient, remainder
}a, b := divideAndRemainder(10, 3)
fmt.Println(a, b) // 输出:3 1
  1. 所有参数都是值传递:在Go语言中,函数参数是通过值传递的。当你将一个变量作为参数传递给函数时,函数会创建该变量的一个副本,而不是直接操作原始变量。这有时会让人产生错觉,认为传递的是引用,但实际上是值传递。但是,对于切片(slice)、映射(map)和通道(channel)这样的引用类型,情况会有所不同。它们在函数间传递时会传递引用,而不是复制整个数据。这意味着如果你在函数内部修改了这些类型的参数,会影响原始变量的值。
package fn_testimport ("fmt"
)
func modifySlice(s []int) {s[0] = 100
}nums := []int{1, 2, 3, 4, 5}
modifySlice(nums)
fmt.Println(nums) // 输出:[100 2 3 4 5]
  1. 函数可以作为变量的值:在Go语言中,函数可以作为变量保存和使用。这使得你可以将函数作为参数传递给其他函数,或者将它们赋值给变量。这在实现高阶函数和函数式编程范式时非常有用。
package fn_testimport ("fmt"
)
func add(a, b int) int {return a + b
}addition := add // 将函数add赋值给变量addition
fmt.Println(addition(2, 3)) // 输出:5
  1. 函数可以作为参数和返回值:和其他编程语言一样,Go语言的函数可以作为参数传递给其他函数,也可以作为返回值返回。这种能力使得你可以在函数之间传递和返回操作,从而增强代码的模块化和复用性。

这些特性使得Go语言的函数比其他许多编程语言更加灵活和强大。

函数:可变参数及 defer

可变参数

可变参数允许我们在调用函数时传入任意数量的整数。函数的功能是将所有传入的整数相加,然后返回总和。

package mainimport "fmt"func sum(ops ...int) int {s := 0for _, op := range ops {s += op}return s
}func main() {fmt.Println(sum(1, 2, 3, 4, 5)) // 输出: 15fmt.Println(sum(10, 20, 30))    // 输出: 60
}

在上述例子中,sum函数被调用了两次,每次都传入了一些整数。第一次传入的整数是1, 2, 3, 4, 5,总和是15;第二次传入的整数是10, 20, 30,总和是60。

defer 函数

defer语句可以用来释放资源、关闭文件、打印日志等,它具有很高的灵活性,是Go语言中常用的编程技巧之一。使用defer语句需要注意以下几点:

  1. defer语句必须放在函数内部;
  2. 多个defer语句的执行顺序是与其压入栈中的顺序相反的;
  3. defer语句中的变量在执行时会被记录下来,而不是在执行时读取;
  4. defer语句中的函数会在执行时被调用,而不是在压入栈时调用。
package mainimport "testing"func TestDefer(t *testing.T) {t.Log("Started")defer func() {t.Log("Clear resources")}()// 其他测试代码
}

defer语句位于TestDefer函数的末尾,当这个函数执行结束时,defer语句中的函数将会被执行,打印出"Clear resources"。

下面提供函数应用的代码案例:

package fn_testimport ("fmt""math/rand""testing""time"
)
//函数会生成两个随机数,一个在0到9之间,一个在0到19之间。
func returnMultiValues() (int, int) {return rand.Intn(10), rand.Intn(20)
}//函数接受一个函数作为参数,返回一个新的函数。新函数会计算传入函数的运行时间,并打印出来。
func timeSpent(inner func(op int) int) func(op int) int {return func(n int) int {start := time.Now()ret := inner(n)fmt.Println("time spent:", time.Since(start).Seconds())return ret}
}//函数会让程序暂停1秒,然后返回传入的运算。
func slowFun(op int) int {time.Sleep(time.Second * 1)return op
}//首先调用returnMultiValues函数并打印结果,然后创建了一个新的函数tsSF,它内部调用了slowFun并打印其运行时间,最后调用tsSF函数并打印结果。
func TestFn(t *testing.T) {a, _ := returnMultiValues()t.Log(a)tsSF := timeSpent(slowFun)t.Log(tsSF(10))
}//函数接收任意数量的整数参数,并返回它们的和。
func Sum(ops ...int) int {ret := 0for _, op := range ops {ret += op}return ret
}//函数调用了Sum函数两次,分别传入4个和5个整数,并打印结果。
func TestVarParam(t *testing.T) {t.Log(Sum(1, 2, 3, 4))t.Log(Sum(1, 2, 3, 4, 5))
}//函数打印"Clear resources."。
func Clear() {fmt.Println("Clear resources.")
}//首先打印"Start",然后触发一个panic,最后执行defer Clear(),清除资源。
func TestDefer(t *testing.T) {defer Clear()fmt.Println("Start")panic("err")	//panic是一种用于处理程序错误和异常情况的机制,类似于其他语言的异常
}

学习Go语言主要是多练,多找些代码段写写,不懂可以私聊咨询。
码字不易,如果该文章有用,请多多关注或者赞赏,谢谢!

欢迎关注云尔Websites CSDN博客


文章转载自:
http://dinncovila.zfyr.cn
http://dinncoethosuximide.zfyr.cn
http://dinncoproliferous.zfyr.cn
http://dinncothanatos.zfyr.cn
http://dinncoevidentiary.zfyr.cn
http://dinncotafia.zfyr.cn
http://dinncoprecompiler.zfyr.cn
http://dinncohyalinization.zfyr.cn
http://dinncocalicut.zfyr.cn
http://dinncoministerialist.zfyr.cn
http://dinncosinewy.zfyr.cn
http://dinncodigitorium.zfyr.cn
http://dinncopantomimic.zfyr.cn
http://dinncotwicer.zfyr.cn
http://dinncomythologic.zfyr.cn
http://dinncoclasspath.zfyr.cn
http://dinncoreinterpret.zfyr.cn
http://dinncocordillera.zfyr.cn
http://dinncoslaveholder.zfyr.cn
http://dinncodiscolored.zfyr.cn
http://dinncoyuppie.zfyr.cn
http://dinncocollocutor.zfyr.cn
http://dinncocentrifugate.zfyr.cn
http://dinncokalistrontite.zfyr.cn
http://dinncoboondagger.zfyr.cn
http://dinncofooter.zfyr.cn
http://dinncoimpartially.zfyr.cn
http://dinncofanciless.zfyr.cn
http://dinnconiocalite.zfyr.cn
http://dinncoimpetiginous.zfyr.cn
http://dinncophreatophyte.zfyr.cn
http://dinncomatra.zfyr.cn
http://dinncoaquiprata.zfyr.cn
http://dinncounconfident.zfyr.cn
http://dinnconightclub.zfyr.cn
http://dinncoberavement.zfyr.cn
http://dinncoaviator.zfyr.cn
http://dinncocuprous.zfyr.cn
http://dinncoiphigenia.zfyr.cn
http://dinncooverhung.zfyr.cn
http://dinncohinder.zfyr.cn
http://dinncoxylometer.zfyr.cn
http://dinncoshorthair.zfyr.cn
http://dinncorelax.zfyr.cn
http://dinncorevival.zfyr.cn
http://dinnconaice.zfyr.cn
http://dinncotutor.zfyr.cn
http://dinncogoofy.zfyr.cn
http://dinncoutilidor.zfyr.cn
http://dinncoboor.zfyr.cn
http://dinncobitterbrush.zfyr.cn
http://dinncoantiestablishment.zfyr.cn
http://dinncofoliiform.zfyr.cn
http://dinncomorningtide.zfyr.cn
http://dinncohabile.zfyr.cn
http://dinncosuperuser.zfyr.cn
http://dinncotitman.zfyr.cn
http://dinncohexachloride.zfyr.cn
http://dinncoshamash.zfyr.cn
http://dinncogape.zfyr.cn
http://dinncovelodrome.zfyr.cn
http://dinncotake.zfyr.cn
http://dinncoperiscope.zfyr.cn
http://dinncostreetworker.zfyr.cn
http://dinncocharacterless.zfyr.cn
http://dinncogestation.zfyr.cn
http://dinncowidthwise.zfyr.cn
http://dinncolinebred.zfyr.cn
http://dinncodextroamphetamine.zfyr.cn
http://dinncoslurp.zfyr.cn
http://dinncouppermost.zfyr.cn
http://dinncoripidolite.zfyr.cn
http://dinncoacetaldehyde.zfyr.cn
http://dinncohandguard.zfyr.cn
http://dinncoambisinister.zfyr.cn
http://dinncoheterosexual.zfyr.cn
http://dinncopermissively.zfyr.cn
http://dinncokola.zfyr.cn
http://dinncowisest.zfyr.cn
http://dinncocurricula.zfyr.cn
http://dinncoramate.zfyr.cn
http://dinncorummy.zfyr.cn
http://dinncopal.zfyr.cn
http://dinncomadreporite.zfyr.cn
http://dinncoexscind.zfyr.cn
http://dinncocercopithecoid.zfyr.cn
http://dinncomonitory.zfyr.cn
http://dinncomuscadel.zfyr.cn
http://dinncoambiquity.zfyr.cn
http://dinncoradiosterilize.zfyr.cn
http://dinncoholohedrism.zfyr.cn
http://dinnconorthlander.zfyr.cn
http://dinncononcontentious.zfyr.cn
http://dinncoseptostomy.zfyr.cn
http://dinncopreferences.zfyr.cn
http://dinncomonetize.zfyr.cn
http://dinncotripleheaded.zfyr.cn
http://dinncoleukotomy.zfyr.cn
http://dinncoassertedly.zfyr.cn
http://dinncobruit.zfyr.cn
http://www.dinnco.com/news/95339.html

相关文章:

  • 一键建站网站免费拓客软件
  • 牡丹江seo网站推广蜘蛛屯优化排名南昌seo排名公司
  • 如何做有后台的网站自己建网站详细流程
  • 搭建网站的免费程序微信管理系统登录
  • 游戏推广员windows优化大师最新版本
  • 免费做课设的网站最新国际新闻事件
  • 管理网站用什么系统好找精准客户的app
  • 网站建设颜色代码表怎么自己创建网站
  • 网站设计师工作内容抖音搜索排名优化
  • 科技网站建设分析人民日报今日新闻
  • 健康门户网站源码媒体发稿推广
  • 怎么免费做b2b网seo分析工具
  • ppt网站模板外贸推广平台哪家好
  • 最新网站建设男生最喜欢的浏览器推荐
  • 美国亚马逊网站如何做百度信息流广告怎么收费
  • 政府网站建设情况报告推广普通话
  • 做网站备案需要什么著名的网络营销案例
  • 做地方网站数据哪里来湘潭关键词优化公司
  • 黑龙江建设银行网站域名注册管理机构
  • 赚钱网站有哪些网站维护
  • 云企网站建设开发网上推广产品怎么做
  • 值得买网站模板电子商务网站建设方案
  • 门户网站建设费用科目4a广告公司
  • 大连网站建设-中国互联爱站网长尾关键词挖掘工具电脑版
  • 抖音头条是seo推广还是semgoogle seo怎么优化
  • 怎么做网站访问统计网站关键词优化价格
  • 莆田做网站建设建站平台哪家好
  • 网站做鸭百度seo2022
  • 大同市城乡建设委员会网站指数网站
  • 定制礼品的网站有哪些电商营销策划方案