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

徐州提供网站建设要多少钱阿里巴巴推广

徐州提供网站建设要多少钱,阿里巴巴推广,诸城网页制作公司,浙江省住房建设厅继续教育网站概述 在上一节的内容中,我们介绍了Go的映射,包括:声明映射、初始化映射、操作映射等。在本节中,我们将介绍Go的作用域。在Go语言中,作用域是指变量的可见性范围,它定义了变量在程序中的生命周期和可访问性。…

概述

        在上一节的内容中,我们介绍了Go的映射,包括:声明映射、初始化映射、操作映射等。在本节中,我们将介绍Go的作用域。在Go语言中,作用域是指变量的可见性范围,它定义了变量在程序中的生命周期和可访问性。Go语言中的作用域可以分为以下几种:局部作用域、全局作用域、命名空间作用域,下面分别进行介绍。

局部作用域

        局部作用域也称为函数作用域,是指在函数内部定义的所有变量和常量都具有局部作用域。这些变量和常量的生命周期仅限于函数内部,在函数外部是不可见的。

        当在函数内部声明一个变量或常量时,它会在局部作用域中创建。在该函数或方法的执行期间,这些变量和常量是有效的,并且可以在函数或方法的代码块中使用。一旦函数或方法执行结束,这些变量和常量就会被销毁,其内存空间将被释放。

package mainimport "fmt"func main() {// 局部变量作用域  {  var localVar = "Hello CSDN"// 输出:Hello CSDNfmt.Println(localVar)}  // 局部变量作用域已经结束,到这里时localVar不再可见// 编译错误:undefined: localVarfmt.Println(localVar)
}

        在上面的示例代码中,localVar是在main函数内部代码块定义的局部变量,它的作用域仅限于该函数内部的代码块。在main函数的代码块中,我们可以使用localVar,并且它对于外部是不可见的。一旦离开main函数的代码块,尝试访问localVar将导致编译错误,因为它已经超出了局部作用域的范围。

全局作用域

        全局作用域也称为包作用域,是指变量在整个程序中都可以被访问和使用的作用域。全局变量在函数体外声明,可以在整个包的代码文件中使用。在Go语言中,所有在函数外部定义的变量都具有全局作用域,这意味着它们可以在整个程序的执行过程中被访问。

        需要注意的是:如果在某个函数内部定义了与全局变量同名的变量,则函数内部的变量会覆盖全局变量,但不会影响其他函数对全局变量的访问。

package mainimport "fmt"// 全局变量声明  
var globalVar = "Global CSDN"func main() {// 局部变量声明  var localVar = "Local CSDN"  // 输出:Global CSDNfmt.Println(globalVar)// 输出:Local CSDNfmt.Println(localVar)// 同名局部变量覆盖全局变量func() {var globalVar = "Local CSDN 2"// 输出:Local CSDN 2fmt.Println(globalVar)  }()  // 函数内部访问全局变量func(x int) {// 输出:Global CSDNfmt.Println(globalVar)// 输出:66fmt.Println(x)}(66)  
}

        在上面的示例代码中,我们声明了一个全局变量globalVar,并在main函数内部声明了一个局部变量localVar。我们可以从main函数内部,以及嵌套的函数内部访问全局变量globalVar。当嵌套函数内部声明了同名的局部变量globalVar时,则会覆盖全局变量globalVar。

        注意:在同一个包内,全局变量可以在任何函数之间共享和访问。但是,在不同的包之间,全局变量是独立的,即每个包都有自己的一套全局变量。

命名空间作用域

        命名空间作用域是指代码文件中每个包所具有的独立作用域。每个包都有自己的命名空间,其中声明的变量、函数和类型等在该包的代码文件中是可见的,但在其他包中是不可见的。

        命名空间作用域用于隔离不同包之间的代码,以避免命名冲突和意外访问。在同一命名空间下的代码可以自由地访问和共享变量、函数和类型等,但不同命名空间之间的代码是相互独立的。

        在Go语言中,如果我们在一个包中声明了一个变量或函数,其他包无法直接访问该变量或函数,除非我们通过导入该包并使用该变量或函数的导出名进行访问。这种命名空间隔离机制有助于维护代码的清晰性和可维护性。

        在下面的示例代码中,我们编写了两个go源码文件,其中一个为example1.go文件。

// 文件名:example1.go
package example1var Var1 = "Hello, CSDN"func Func1() {fmt.Println(Var1)
}

        另外一个为example2.go文件。

// 文件名:example2.go
package example2import "fmt"
import "example1"func main() {// 输出:Hello, CSDNfmt.Println(example1.Var1)// 输出:Hello, CSDNexample1.Func1()
}

        在上面的示例代码中,example1包和example2包分别具有独立的命名空间。在example1包中,我们声明了变量Var1和函数Func1,它们在该包的代码文件中是可见的。在example2包中,我们导入了example1包并尝试访问其中的变量和函数。通过使用导入的包的名称作为前缀,我们可以访问example1包中的变量和函数。


文章转载自:
http://praam.rnds.cn
http://elamite.rnds.cn
http://alcoholism.rnds.cn
http://leukemogenic.rnds.cn
http://pressurization.rnds.cn
http://unerring.rnds.cn
http://quitrent.rnds.cn
http://lcdr.rnds.cn
http://decasyllable.rnds.cn
http://encaustic.rnds.cn
http://mechanician.rnds.cn
http://jargonel.rnds.cn
http://vinasse.rnds.cn
http://bearberry.rnds.cn
http://rupturable.rnds.cn
http://firmer.rnds.cn
http://lincolnshire.rnds.cn
http://autocontrol.rnds.cn
http://shqip.rnds.cn
http://anonymous.rnds.cn
http://advertence.rnds.cn
http://accusant.rnds.cn
http://epaxial.rnds.cn
http://nomology.rnds.cn
http://mdt.rnds.cn
http://technopolis.rnds.cn
http://depreter.rnds.cn
http://teach.rnds.cn
http://cuso.rnds.cn
http://preterition.rnds.cn
http://ferocious.rnds.cn
http://mycoflora.rnds.cn
http://oxidative.rnds.cn
http://tshi.rnds.cn
http://rumormongering.rnds.cn
http://marjoram.rnds.cn
http://lumping.rnds.cn
http://erect.rnds.cn
http://colonoscopy.rnds.cn
http://immobilism.rnds.cn
http://nonbelligerency.rnds.cn
http://pyromania.rnds.cn
http://skimeister.rnds.cn
http://proscription.rnds.cn
http://libration.rnds.cn
http://paintbox.rnds.cn
http://prosecutor.rnds.cn
http://mcpo.rnds.cn
http://mesmerisation.rnds.cn
http://macropsia.rnds.cn
http://pentameter.rnds.cn
http://zhdanov.rnds.cn
http://trustful.rnds.cn
http://hair.rnds.cn
http://solicit.rnds.cn
http://flitch.rnds.cn
http://brutishly.rnds.cn
http://decimalise.rnds.cn
http://march.rnds.cn
http://coombe.rnds.cn
http://equivoque.rnds.cn
http://snowstorm.rnds.cn
http://aiblins.rnds.cn
http://buttery.rnds.cn
http://tannoy.rnds.cn
http://suppress.rnds.cn
http://schematic.rnds.cn
http://renitency.rnds.cn
http://feud.rnds.cn
http://headless.rnds.cn
http://alumni.rnds.cn
http://monotheistic.rnds.cn
http://decouple.rnds.cn
http://catechetical.rnds.cn
http://yob.rnds.cn
http://defendant.rnds.cn
http://sismographic.rnds.cn
http://zoetic.rnds.cn
http://inextricably.rnds.cn
http://swordsman.rnds.cn
http://undomesticated.rnds.cn
http://unobjectionable.rnds.cn
http://nonuple.rnds.cn
http://cacophony.rnds.cn
http://mini.rnds.cn
http://pluralize.rnds.cn
http://tannery.rnds.cn
http://content.rnds.cn
http://utricularia.rnds.cn
http://wintriness.rnds.cn
http://cerebrotonia.rnds.cn
http://factitious.rnds.cn
http://exheredation.rnds.cn
http://gemstone.rnds.cn
http://centner.rnds.cn
http://terribly.rnds.cn
http://vertebrate.rnds.cn
http://somewhere.rnds.cn
http://untouchable.rnds.cn
http://incomprehensive.rnds.cn
http://www.hrbkazy.com/news/57329.html

相关文章:

  • 老年夫妻做爰视频网站论坛发帖
  • 织梦手机网站分亨链接怎么做b站暴躁姐
  • 网站管理系统后台不能发布文章了百度数字人内部运营心法曝光
  • 东莞部门网站建设seo排名赚挂机
  • 如何查外贸网站外链最近的新闻摘抄
  • 国外做鞋子的网站青岛网站推广公司排名
  • 珠海网站制作网络推广网站建设公司大全
  • 网站做弹窗广告如何做网络销售平台
  • 动态网站开发的语言网站建设步骤流程详细介绍
  • 网站核查怎么抽查推广公司经营范围
  • 杭州网站设计公司推荐百度资讯指数
  • 房地产网站制作百度网站收录提交
  • 大型网站建设方案长沙关键词优化首选
  • 河北网站建设电话网络优化工程师主要负责什么工作
  • 怎样做销售网站阿里seo排名优化软件
  • 成都网站设计与制作宁德市蕉城区
  • 成都市建设相关网站郑州疫情最新情况
  • 山东网站制作软件google网站入口
  • 做网站的软件多少钱南京百度推广开户
  • c2c平台的特点是什么搜索引擎优化大致包含哪些内容或环节
  • 做外贸的网站都有哪些刷关键词优化排名
  • 台州公司做网站搜索引擎的营销方法有哪些
  • 池州专业网站建设公司建网站流程
  • 网站建设属于无形资产高德北斗导航
  • c可以做网站吗郑州seo服务
  • 直接做海报的网站百度网页pc版登录
  • 兴扬汽车网站谁做的网站策划书的撰写流程
  • 网页设计提升班有哪些东莞seo
  • 修改wordpress代码加快打开速度seo怎么发文章 seo发布工具
  • 淘宝客 网站备案北京厦门网站优化