[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"site-config":3,"primary-menu":6,"cat-golang":85,"catposts-golang":97,"post-detail-post-golang-slice-f8bb13e6":795},{"config":4,"socialLinks":5},null,[],{"metadata":7,"spec":10,"menuItems":20},{"name":8,"creationTimestamp":9},"primary","2026-09-12T06:13:55.024154Z",{"displayName":11,"menuItems":12},"主菜单",[13,14,15,16,17,18,19],"88c3f10b-321c-4092-86a8-70db00251b74","menu-item-izrtmxer","c4c814d1-0c2c-456b-8c96-4864965fee94","menu-item-column","menu-item-tools","menu-item-zsxq","menu-item-ocrxlczu",[21,30,40,49,58,67,76],{"displayName":22,"metadata":23,"spec":25,"status":28,"children":29},"首页",{"name":13,"creationTimestamp":24},"2026-09-12T06:13:54.422830Z",{"displayName":22,"href":26,"priority":27,"menuName":8},"\u002F",0,{"displayName":22,"href":26},[],{"displayName":31,"metadata":32,"spec":34,"status":38,"children":39},"Java面试八股",{"name":14,"creationTimestamp":33},"2026-09-13T09:42:02.854891Z",{"displayName":31,"href":35,"target":36,"priority":37,"menuName":8},"\u002Fjava-interview","_self",1,{"displayName":31,"href":35},[],{"displayName":41,"metadata":42,"spec":44,"status":47,"children":48},"文章",{"name":15,"creationTimestamp":43},"2026-09-12T06:13:54.457307Z",{"displayName":41,"href":45,"priority":46,"menuName":8},"\u002Farchives",2,{"displayName":41,"href":45},[],{"displayName":50,"metadata":51,"spec":53,"status":56,"children":57},"项目实战",{"name":16,"creationTimestamp":52},"2026-09-14T00:35:08.138170Z",{"displayName":50,"href":54,"target":36,"priority":55,"menuName":8},"\u002Fcolumn",3,{"displayName":50,"href":54},[],{"displayName":59,"metadata":60,"spec":62,"status":65,"children":66},"在线工具",{"name":17,"creationTimestamp":61},"2026-09-14T00:35:08.171474Z",{"displayName":59,"href":63,"target":36,"priority":64,"menuName":8},"\u002Ftools",4,{"displayName":59,"href":63},[],{"displayName":68,"metadata":69,"spec":71,"status":74,"children":75},"知识星球",{"name":18,"creationTimestamp":70},"2026-09-14T00:35:08.199145Z",{"displayName":68,"href":72,"target":36,"priority":73,"menuName":8},"\u002Fzsxq",5,{"displayName":68,"href":72},[],{"displayName":77,"metadata":78,"spec":80,"status":83,"children":84},"特价云服务器",{"name":19,"creationTimestamp":79},"2026-09-25T15:30:59.201614Z",{"displayName":77,"href":81,"target":36,"priority":82,"menuName":8},"\u002Fserver",6,{"displayName":77,"href":81},[],{"metadata":86,"spec":89,"status":94,"postCount":96},{"name":87,"creationTimestamp":88},"golang","2026-09-12T08:21:58.713526Z",{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},"Go 语言教程","\u002Fupload\u002Ftutorial-cover-golang.png","Go 语言零基础到进阶教程：基础语法、接口与错误处理、GMP 并发模型与工程实践。",false,{"permalink":95,"postCount":96,"visiblePostCount":96},"\u002Fcategories\u002Fgolang",33,[98,122,143,164,185,206,227,248,269,290,311,332,353,374,395,416,437,458,479,500,521,542,563,584,605,626,647,668,689,710,731,752,773],{"metadata":99,"spec":102,"status":113,"categories":117},{"name":100,"creationTimestamp":101},"post-windows-install-golang-a21d2168","2026-09-12T08:29:06.344429Z",{"title":103,"slug":104,"cover":105,"excerpt":106,"publishTime":108,"categories":109,"tags":110,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Windows系统安装Go语言环境（图文教程）","windows-install-golang","\u002Fupload\u002Fcover-49509db4b9ba.png",{"raw":107,"autoGenerate":93},"本小节，我将演示如何在 Windows 系统上安装 Go 语言环境。一、下载 Windows 系统 Go 安装包，首先到官网下载 Windows 系统 Go 安装包，详细请参考前面小节内容Go下载，下载完成后，双击安装包开始安装，最后检查 Windows 系统 Go 环境是否安装成功。","2026-09-12T08:29:06.414277Z",[87],[],"PUBLIC",true,{"permalink":114,"excerpt":107,"lastModifyTime":115,"phase":116},"\u002Farchives\u002Fwindows-install-golang","2026-09-14T00:21:01.224634Z","PUBLISHED",[118],{"metadata":119,"spec":120,"status":121,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":123,"spec":126,"status":135,"categories":138},{"name":124,"creationTimestamp":125},"post-macos-install-golang-7efe4d18","2026-09-12T08:29:05.611780Z",{"title":127,"slug":128,"cover":129,"excerpt":130,"publishTime":132,"categories":133,"tags":134,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Mac系统安装Go语言环境（图文教程）","macos-install-golang","\u002Fupload\u002F164870453722926.png",{"raw":131,"autoGenerate":93},"这一小节，我将手摸手教大家如何在 Mac 系统上安装 Go 语言环境。Mac 系统有下面两种安装方式。一、通过 brew 安装 二、官网下载 Go 语言 pkg 包来安装，两种方式均通过图文来给大家讲解，良心教程。\r\n","2026-09-12T08:29:05.685842Z",[87],[],{"permalink":136,"excerpt":131,"lastModifyTime":137,"phase":116},"\u002Farchives\u002Fmacos-install-golang","2026-09-14T00:21:01.518464Z",[139],{"metadata":140,"spec":141,"status":142,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":144,"spec":147,"status":156,"categories":159},{"name":145,"creationTimestamp":146},"post-linux-install-golang-ff2cabb0","2026-09-12T08:29:04.983033Z",{"title":148,"slug":149,"cover":150,"excerpt":151,"publishTime":153,"categories":154,"tags":155,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Linux系统安装Go语言环境（图文教程）","linux-install-golang","\u002Fupload\u002Fcover-cd7d36020297.png",{"raw":152,"autoGenerate":93},"本小节，我将演示如何在 Linux 系统上安装 Go 语言环境。这里，我以自己购买的云服务器 CentOS 7 做演示。首先到官网下载 Linux 系统 Go 安装包，下载完成后，将压缩包上传到指定目录下，执行如下命令，将压缩包解压到 \u002Fusr\u002Flocal\u002Fgo 下...","2026-09-12T08:29:05.039772Z",[87],[],{"permalink":157,"excerpt":152,"lastModifyTime":158,"phase":116},"\u002Farchives\u002Flinux-install-golang","2026-09-14T00:21:02.058274Z",[160],{"metadata":161,"spec":162,"status":163,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":165,"spec":168,"status":177,"categories":180},{"name":166,"creationTimestamp":167},"post-install-jetbrains-golang-4b354194","2026-09-12T08:29:04.389267Z",{"title":169,"slug":170,"cover":171,"excerpt":172,"publishTime":174,"categories":175,"tags":176,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Jetbrains GoLang 安装&amp;开发环境搭建","install-jetbrains-golang","\u002Fupload\u002F164870486582118.png",{"raw":173,"autoGenerate":93},"Jetbrains GoLang 是一款面向 Go 语言，旨在为 Go 开发提供符合人体工程学的开发工具。它提供 Go 语言的编辑、调试、编译、管理、重构等丰富的功能。本小节，我们将学习安装搭建集成开发环境Jetbrains GoLang, 并通过它来开发一个简单的 HelloWorld 程序！","2026-09-12T08:29:04.440937Z",[87],[],{"permalink":178,"excerpt":173,"lastModifyTime":179,"phase":116},"\u002Farchives\u002Finstall-jetbrains-golang","2026-09-13T05:17:54.053255Z",[181],{"metadata":182,"spec":183,"status":184,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":186,"spec":189,"status":198,"categories":201},{"name":187,"creationTimestamp":188},"post-golang-variables-55d7b2af","2026-09-12T08:29:03.751627Z",{"title":190,"slug":191,"cover":192,"excerpt":193,"publishTime":195,"categories":196,"tags":197,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言变量声明_定义_赋值（图文讲解）","golang-variables","\u002Fupload\u002F164870897498294.png",{"raw":194,"autoGenerate":93},"编程中可以说最常见的就是变量了，主要被用来存储各种类型数据。关于变量的类型，主要有: 整型浮、点型、布尔型、结构体等。Go 语言中不同类型的变量，需要先声明，才能够被使用。","2026-09-12T08:29:03.813668Z",[87],[],{"permalink":199,"excerpt":194,"lastModifyTime":200,"phase":116},"\u002Farchives\u002Fgolang-variables","2026-09-13T05:17:54.598729Z",[202],{"metadata":203,"spec":204,"status":205,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":207,"spec":210,"status":219,"categories":222},{"name":208,"creationTimestamp":209},"post-golang-switch-a10b005c","2026-09-12T08:29:03.106098Z",{"title":211,"slug":212,"cover":213,"excerpt":214,"publishTime":216,"categories":217,"tags":218,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言 switch 语句（分支判断）","golang-switch","\u002Fupload\u002F164871010411752.png",{"raw":215,"autoGenerate":93},"分支判断可以理解成批量的 if 语句，使用 switch 语句可以非常方便地对大量值进行条件判断。Go 语言改进了其他语言中 switch 语句的设计弊端。Go 语言中 switch 中的每一个 case 都是独立代码块，无需通过 break 语句跳出代码块，以避免执行到下一个 case 代码块 ...\r\n\r\n\r\n","2026-09-12T08:29:03.172911Z",[87],[],{"permalink":220,"excerpt":215,"lastModifyTime":221,"phase":116},"\u002Farchives\u002Fgolang-switch","2026-09-13T05:17:55.423312Z",[223],{"metadata":224,"spec":225,"status":226,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":228,"spec":231,"status":240,"categories":243},{"name":229,"creationTimestamp":230},"post-golang-slice-f8bb13e6","2026-09-12T08:29:02.530409Z",{"title":232,"slug":233,"cover":234,"excerpt":235,"publishTime":237,"categories":238,"tags":239,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言切片（Slice）初始化_删除元素_遍历","golang-slice","\u002Fupload\u002F164870974706279.png",{"raw":236,"autoGenerate":93},"切片和数组类似，是数据集合。和数组不同的是，切片是一块动态分配大小的连续空间。它和 Java 语言中的 List 集合类似。切片的声明格式如下:var name []T name 表示切片变量名，T 表示切片类型，下面是示例代码 ...","2026-09-12T08:29:02.594243Z",[87],[],{"permalink":241,"excerpt":236,"lastModifyTime":242,"phase":116},"\u002Farchives\u002Fgolang-slice","2026-09-13T05:17:56.609115Z",[244],{"metadata":245,"spec":246,"status":247,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":249,"spec":252,"status":261,"categories":264},{"name":250,"creationTimestamp":251},"post-golang-pointer-157d7d47","2026-09-12T08:29:01.780678Z",{"title":253,"slug":254,"cover":255,"excerpt":256,"publishTime":258,"categories":259,"tags":260,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言指针 (图文教程，超级详细)","golang-pointer","\u002Fupload\u002F164870962404380.png",{"raw":257,"autoGenerate":93},"大部分面向对象语言都很少有用到指针的场景了，但是在Go语言中有大量的指针应用场景，要想学好 Go 语言，指针是必须了解的。本小节中，就将学习 Go 语言指针相关知识点。每一个变量都会分配一块内存，数据保存在内存中，内存有一个地址，就像门牌号，通过这个地址就可以找到里面存储的数据。指针就是保存这个内存地址的变量。\r\n\r\n","2026-09-12T08:29:01.949985Z",[87],[],{"permalink":262,"excerpt":257,"lastModifyTime":263,"phase":116},"\u002Farchives\u002Fgolang-pointer","2026-09-13T05:17:58.376696Z",[265],{"metadata":266,"spec":267,"status":268,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":270,"spec":273,"status":282,"categories":285},{"name":271,"creationTimestamp":272},"post-golang-map-4643324e","2026-09-12T08:29:01.129328Z",{"title":274,"slug":275,"cover":276,"excerpt":277,"publishTime":279,"categories":280,"tags":281,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言字典 (Map)","golang-map","\u002Fupload\u002F164870982152632.png",{"raw":278,"autoGenerate":93},"很多场景下都需要字典容器，它描述的是一种映射关系，如字典中的页码对应相关页内容一样。在 Go 语言中提供的字典容器为 map。 map 使用散列表（hash）实现。Go 语言中定义字典 map 格式如下: map [keyType]valueType ,键和键对应的值总是以一对一的形式存在 ...\r\n","2026-09-12T08:29:01.199026Z",[87],[],{"permalink":283,"excerpt":278,"lastModifyTime":284,"phase":116},"\u002Farchives\u002Fgolang-map","2026-09-13T05:17:59.383462Z",[286],{"metadata":287,"spec":288,"status":289,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":291,"spec":294,"status":303,"categories":306},{"name":292,"creationTimestamp":293},"post-golang-list-bfb63978","2026-09-12T08:29:00.459682Z",{"title":295,"slug":296,"cover":297,"excerpt":298,"publishTime":300,"categories":301,"tags":302,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言 list (列表)_初始化_遍历_删除","golang-list","\u002Fupload\u002F164870987319337.png",{"raw":299,"autoGenerate":93},"列表 (list) 是一种非连续存储的容器，又多个节点组成，节点通过一些变量将彼此串联起来。列表（list）底层常见的数据结构有: 单链表、双链表等。在 Go 语言中，列表的实现都在 container\u002Flist 包中，内部实现原理是双链表。列表（list）能够方便高效地进行元素的删除、插入操作。\r\n","2026-09-12T08:29:00.524219Z",[87],[],{"permalink":304,"excerpt":299,"lastModifyTime":305,"phase":116},"\u002Farchives\u002Fgolang-list","2026-09-13T05:18:00.756584Z",[307],{"metadata":308,"spec":309,"status":310,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":312,"spec":315,"status":324,"categories":327},{"name":313,"creationTimestamp":314},"post-golang-if-else-c59fa420","2026-09-12T08:28:59.871285Z",{"title":316,"slug":317,"cover":318,"excerpt":319,"publishTime":321,"categories":322,"tags":323,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言 if else (条件判断)","golang-if-else","\u002Fupload\u002F164870991767712.png",{"raw":320,"autoGenerate":93},"Go 语言中，if else 条件判断格式如下：if 表达式1 { 分支1 } else if 表达式2 { 分支2 } else { 分支3 } ，当表达式1的结果为 true 时，会执行分支1的代码；当表达式1的结果为 false 时，会执行表达式2，若结果为 true, 则会执行分支2的代码...","2026-09-12T08:28:59.929025Z",[87],[],{"permalink":325,"excerpt":320,"lastModifyTime":326,"phase":116},"\u002Farchives\u002Fgolang-if-else","2026-09-13T05:18:01.594113Z",[328],{"metadata":329,"spec":330,"status":331,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":333,"spec":336,"status":345,"categories":348},{"name":334,"creationTimestamp":335},"post-golang-hello-world-cf894aff","2026-09-12T08:28:59.215647Z",{"title":337,"slug":338,"cover":339,"excerpt":340,"publishTime":342,"categories":343,"tags":344,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"编写 Go 语言第一个程序: Hello World ！","golang-hello-world","\u002Fupload\u002F164870471270526.png",{"raw":341,"autoGenerate":93},"通常来说，入门任何一门语言，编写的第一个程序都是用该语言向世界打声招呼： Hello World ! 接下来，我们来编写第一个 Go 语言程序，新建一个 helloworld.go 文件，代码如下 ...","2026-09-12T08:28:59.273231Z",[87],[],{"permalink":346,"excerpt":341,"lastModifyTime":347,"phase":116},"\u002Farchives\u002Fgolang-hello-world","2026-09-13T05:18:02.048325Z",[349],{"metadata":350,"spec":351,"status":352,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":354,"spec":357,"status":366,"categories":369},{"name":355,"creationTimestamp":356},"post-golang-goto-4acea4b8","2026-09-12T08:28:58.582637Z",{"title":358,"slug":359,"cover":360,"excerpt":361,"publishTime":363,"categories":364,"tags":365,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言 goto 语句（代码跳转）","golang-goto","\u002Fupload\u002F164871014638160.png",{"raw":362,"autoGenerate":93},"在 Go 语言中，goto 语句通过标签实现代码间的跳转。它通常会被使用在快速跳出循环、避免定义重复代码等 ...","2026-09-12T08:28:58.638534Z",[87],[],{"permalink":367,"excerpt":362,"lastModifyTime":368,"phase":116},"\u002Farchives\u002Fgolang-goto","2026-09-13T05:18:03.032618Z",[370],{"metadata":371,"spec":372,"status":373,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":375,"spec":378,"status":387,"categories":390},{"name":376,"creationTimestamp":377},"post-golang-function-ef90cbc7","2026-09-12T08:28:57.735525Z",{"title":379,"slug":380,"cover":381,"excerpt":382,"publishTime":384,"categories":385,"tags":386,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言函数","golang-function","\u002Fupload\u002F164871026284043.jpg",{"raw":383,"autoGenerate":93},"在编程中，函数是指一段可以直接被另一段程序或代码引用的、可重复使用的、用来实现单一或相关联功能的代码段。目的是为了提高应用的模块性和代码的重复利用率。相比较其他语言，Go 语言在设计上对函数进行了优化和改进，使其使用起来更加便利 ...","2026-09-12T08:28:57.796658Z",[87],[],{"permalink":388,"excerpt":383,"lastModifyTime":389,"phase":116},"\u002Farchives\u002Fgolang-function","2026-09-13T05:18:03.578539Z",[391],{"metadata":392,"spec":393,"status":394,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":396,"spec":399,"status":408,"categories":411},{"name":397,"creationTimestamp":398},"post-golang-function-variable-94ff5e53","2026-09-12T08:28:57.125546Z",{"title":400,"slug":401,"cover":402,"excerpt":403,"publishTime":405,"categories":406,"tags":407,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言函数变量（将函数保存到变量中）","golang-function-variable","\u002Fupload\u002F164871038499644.png",{"raw":404,"autoGenerate":93},"在 Go 语言中，函数也是一种类型，同样可以和其他类型（如 int 、float、string 等）一样被保存到变量中。示例代码如下 ...","2026-09-12T08:28:57.195876Z",[87],[],{"permalink":409,"excerpt":404,"lastModifyTime":410,"phase":116},"\u002Farchives\u002Fgolang-function-variable","2026-09-13T05:18:04.468273Z",[412],{"metadata":413,"spec":414,"status":415,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":417,"spec":420,"status":429,"categories":432},{"name":418,"creationTimestamp":419},"post-golang-function-interface-058ccba7","2026-09-12T08:28:56.555615Z",{"title":421,"slug":422,"cover":423,"excerpt":424,"publishTime":426,"categories":427,"tags":428,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言函数实现接口（将函数当做接口来调用）","golang-function-interface","\u002Fupload\u002F164871047596556.png",{"raw":425,"autoGenerate":93},"在 Go 语言中，其他基本类型能够实现接口，函数同样可以实现接口。下面我们定义一个名为狗 Dog 的接口，上面这个接口需要实现 Say() 方法，调用时需传入一个 interface{} 类型的变量。这是个啥类型呢？这种类型表示你可以传入任意类型的值...","2026-09-12T08:28:56.614447Z",[87],[],{"permalink":430,"excerpt":425,"lastModifyTime":431,"phase":116},"\u002Farchives\u002Fgolang-function-interface","2026-09-13T05:18:05.479619Z",[433],{"metadata":434,"spec":435,"status":436,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":438,"spec":441,"status":450,"categories":453},{"name":439,"creationTimestamp":440},"post-golang-function-define-bc188aff","2026-09-12T08:28:55.987596Z",{"title":442,"slug":443,"cover":444,"excerpt":445,"publishTime":447,"categories":448,"tags":449,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言函数定义_声明_调用（超详细）","golang-function-define","\u002Fupload\u002F164871033125097.png",{"raw":446,"autoGenerate":93},"在 Go 语言中，定义一个函数需要声明参数和函数名等。定义函数需要以 func 标识开头，后面紧接着函数名、参数列表、返回参数列表以及函数体，函数名的命名有以下约束：函数名由字母、数字、下划线组成; 函数名不能以数字开头; 同一包内，函数名不能重名...\r\n","2026-09-12T08:28:56.047485Z",[87],[],{"permalink":451,"excerpt":446,"lastModifyTime":452,"phase":116},"\u002Farchives\u002Fgolang-function-define","2026-09-13T05:18:06.326689Z",[454],{"metadata":455,"spec":456,"status":457,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":459,"spec":462,"status":471,"categories":474},{"name":460,"creationTimestamp":461},"post-golang-for-0e1bdb22","2026-09-12T08:28:55.353775Z",{"title":463,"slug":464,"cover":465,"excerpt":466,"publishTime":468,"categories":469,"tags":470,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言 for（循环）","golang-for","\u002Fupload\u002F164870996521899.png",{"raw":467,"autoGenerate":93},"在 Go 语言中，循环需要使用关键字 for 来完成，格式如下:for 初始语句;条件表达式;结束语句 { 循环体代码}，for 循环会不停地进行循环，直到条件表达式返回 false 才推出循环，即执行 for 循环 &quot;}&quot; 后面的代码...","2026-09-12T08:28:55.435382Z",[87],[],{"permalink":472,"excerpt":467,"lastModifyTime":473,"phase":116},"\u002Farchives\u002Fgolang-for","2026-09-13T05:18:07.480855Z",[475],{"metadata":476,"spec":477,"status":478,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":480,"spec":483,"status":492,"categories":495},{"name":481,"creationTimestamp":482},"post-golang-for-range-b66ec19c","2026-09-12T08:28:54.498447Z",{"title":484,"slug":485,"cover":486,"excerpt":487,"publishTime":489,"categories":490,"tags":491,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言 for range (键值循环)","golang-for-range","\u002Fupload\u002F164871003843896.png",{"raw":488,"autoGenerate":93},"Go 语言中，我们还可以通过 for range 来遍历数组、切片、字符串、map 以及通道（channel）。通过 for range 遍历数组、切片、字符串的返回值都有一个规律：下标索引 index；下标对应的值 value；下面代码演示了如何遍历切片，数组格式类似 ...","2026-09-12T08:28:54.562781Z",[87],[],{"permalink":493,"excerpt":488,"lastModifyTime":494,"phase":116},"\u002Farchives\u002Fgolang-for-range","2026-09-13T05:18:08.345742Z",[496],{"metadata":497,"spec":498,"status":499,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":501,"spec":504,"status":513,"categories":516},{"name":502,"creationTimestamp":503},"post-golang-enum-iota-677c3ce9","2026-09-12T08:28:53.832167Z",{"title":505,"slug":506,"cover":507,"excerpt":508,"publishTime":510,"categories":511,"tags":512,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言枚举类型（iota自增）","golang-enum-iota","\u002Fupload\u002F164870953934678.png",{"raw":509,"autoGenerate":93},"Go 语言中其实是没有枚举类型的，但是，可以使用常量 iota 来模拟枚举。const 声明内的每一行常量声明，将会自动套用前面 iota 格式，并自动增加。有点类似于 Excel 表格自动填充功能。也就是说，第一行 Woman 枚举值定义了 0， 那么 Man 的值会自动加一，值变成了1。\r\n","2026-09-12T08:28:53.892261Z",[87],[],{"permalink":514,"excerpt":509,"lastModifyTime":515,"phase":116},"\u002Farchives\u002Fgolang-enum-iota","2026-09-13T05:18:10.763173Z",[517],{"metadata":518,"spec":519,"status":520,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":522,"spec":525,"status":534,"categories":537},{"name":523,"creationTimestamp":524},"post-golang-data-type-string-76c51396","2026-09-12T08:28:53.208004Z",{"title":526,"slug":527,"cover":528,"excerpt":529,"publishTime":531,"categories":532,"tags":533,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言字符串 (超级详细)","golang-data-type-string","\u002Fupload\u002F164870932098953.png",{"raw":530,"autoGenerate":93},"在 Go 语言中，字符串是一个不可改变的字节序列，类型为原生数据类型，同 int 、bool、float32、float64 是一样的。字符串的值通过双引号来包裹，Go 语言中，我们可以直接添加非 ASCII 码字符, 代码如下 ...","2026-09-12T08:28:53.273143Z",[87],[],{"permalink":535,"excerpt":530,"lastModifyTime":536,"phase":116},"\u002Farchives\u002Fgolang-data-type-string","2026-09-13T05:18:11.960075Z",[538],{"metadata":539,"spec":540,"status":541,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":543,"spec":546,"status":555,"categories":558},{"name":544,"creationTimestamp":545},"post-golang-data-type-int-9ea8f7dc","2026-09-12T08:28:52.284554Z",{"title":547,"slug":548,"cover":549,"excerpt":550,"publishTime":552,"categories":553,"tags":554,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言整型（整数类型）","golang-data-type-int","\u002Fupload\u002F164870903299782.jpg",{"raw":551,"autoGenerate":93},"在 Go 语言中，整型分为以下两个大类，按长度分为: int8 、int16、int32、int64，对应无符号类型: uint8、uint16、uint32、uint64 ，这里 unit8 就是最小单位 byte型，int16 对应 C 语言中的 short型，int64对应 C 语言中的 long 类型。","2026-09-12T08:28:52.339266Z",[87],[],{"permalink":556,"excerpt":551,"lastModifyTime":557,"phase":116},"\u002Farchives\u002Fgolang-data-type-int","2026-09-13T05:18:13.047546Z",[559],{"metadata":560,"spec":561,"status":562,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":564,"spec":567,"status":576,"categories":579},{"name":565,"creationTimestamp":566},"post-golang-data-type-float-cd45b294","2026-09-12T08:28:51.670525Z",{"title":568,"slug":569,"cover":570,"excerpt":571,"publishTime":573,"categories":574,"tags":575,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言浮点型 (小数类型)","golang-data-type-float","\u002Fupload\u002F164870916250133.png",{"raw":572,"autoGenerate":93},"Go 语言中提供了两种精度浮点型 float32 和 float64。这两种浮点型数据格式遵循 IEEE754 浮点数国际标准，该浮点数规范被所有现代的CPU支持。float32，即我们常说的单精度，存储占用4个字节，也即4*8=32位，其中1位用来符号，8位用来指数，剩下的23位表示尾数...","2026-09-12T08:28:51.731493Z",[87],[],{"permalink":577,"excerpt":572,"lastModifyTime":578,"phase":116},"\u002Farchives\u002Fgolang-data-type-float","2026-09-14T00:21:02.366342Z",[580],{"metadata":581,"spec":582,"status":583,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":585,"spec":588,"status":597,"categories":600},{"name":586,"creationTimestamp":587},"post-golang-data-type-byte-rune-f5b3c021","2026-09-12T08:28:51.045198Z",{"title":589,"slug":590,"cover":591,"excerpt":592,"publishTime":594,"categories":595,"tags":596,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言字符（byte与rune）","golang-data-type-byte-rune","\u002Fupload\u002F164870938414785.png",{"raw":593,"autoGenerate":93},"Go 语言中，字符串的每一个元素叫做字符，主要分为以下两种：1、uint8 类型，或者叫 byte 型，代表了 ASCII 码的一个字符。2、rune 类型，代表了一个 UTF-8 字符。通常情况下，当需要处理中文、日文、韩文等复合字符时，需要用到 rune 类型...","2026-09-12T08:28:51.104406Z",[87],[],{"permalink":598,"excerpt":593,"lastModifyTime":599,"phase":116},"\u002Farchives\u002Fgolang-data-type-byte-rune","2026-09-13T05:18:16.888108Z",[601],{"metadata":602,"spec":603,"status":604,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":606,"spec":609,"status":618,"categories":621},{"name":607,"creationTimestamp":608},"post-golang-data-type-bool-6ff0a48b","2026-09-12T08:28:50.417951Z",{"title":610,"slug":611,"cover":612,"excerpt":613,"publishTime":615,"categories":616,"tags":617,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言布尔型 (bool)","golang-data-type-bool","\u002Fupload\u002F164870922253998.png",{"raw":614,"autoGenerate":93},"在 Go 语言中，以 `bool` 来声明布尔类型， 同时，布尔类型只有 `true` 和 `false` 两种值。if 和 for 语句的条件部分都是布尔类型的值，并且 == 和 &lt; 等比较操作也会产生布尔型的值。一元操作符`!`对应逻辑非操作，因此`!true`的值为`false` ...","2026-09-12T08:28:50.467971Z",[87],[],{"permalink":619,"excerpt":614,"lastModifyTime":620,"phase":116},"\u002Farchives\u002Fgolang-data-type-bool","2026-09-13T05:18:17.535720Z",[622],{"metadata":623,"spec":624,"status":625,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":627,"spec":630,"status":639,"categories":642},{"name":628,"creationTimestamp":629},"post-golang-continue-f0fa1a6c","2026-09-12T08:28:49.801364Z",{"title":631,"slug":632,"cover":633,"excerpt":634,"publishTime":636,"categories":637,"tags":638,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言 continue 语句（继续下次循环）","golang-continue","\u002Fupload\u002F164871022786336.png",{"raw":635,"autoGenerate":93},"在 Go 语言中，continue 语句可以结束当前 for 循环，进入下一次 for 循环, 代码如下 ...","2026-09-12T08:28:49.859649Z",[87],[],{"permalink":640,"excerpt":635,"lastModifyTime":641,"phase":116},"\u002Farchives\u002Fgolang-continue","2026-09-13T05:18:18.463890Z",[643],{"metadata":644,"spec":645,"status":646,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":648,"spec":651,"status":660,"categories":663},{"name":649,"creationTimestamp":650},"post-golang-const-fe7071c3","2026-09-12T08:28:49.202451Z",{"title":652,"slug":653,"cover":654,"excerpt":655,"publishTime":657,"categories":658,"tags":659,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言常量定义与声明 (const)","golang-const","\u002Fupload\u002F164870944501553.png",{"raw":656,"autoGenerate":93},"相对于 Go 语言变量，常量是一个不可改变的值，如圆周率 π 等。常量在代码的编译期就已经确定了，而不是运行时。定义一个 Go 语言常量非常简单，它和变量的声明方式类似，将 var 关键字替换成 const 即可，代码如下...\r\n","2026-09-12T08:28:49.266405Z",[87],[],{"permalink":661,"excerpt":656,"lastModifyTime":662,"phase":116},"\u002Farchives\u002Fgolang-const","2026-09-13T05:18:19.894037Z",[664],{"metadata":665,"spec":666,"status":667,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":669,"spec":672,"status":681,"categories":684},{"name":670,"creationTimestamp":671},"post-golang-closure-function-66448c59","2026-09-12T08:28:48.580216Z",{"title":673,"slug":674,"cover":675,"excerpt":676,"publishTime":678,"categories":679,"tags":680,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言闭包函数_作用_应用场景讲解","golang-closure-function","\u002Fupload\u002F164871051986441.png",{"raw":677,"autoGenerate":93},"在 Go 语言中，闭包是个啥概念呢？一句话来讲：引用了外部变量的匿名函数。公式如下: 函数 + 引用外部变量 = 闭包。一、定义闭包，下面代码演示了如何在 Go 语言中定义闭包 ...\r\n","2026-09-12T08:28:48.641469Z",[87],[],{"permalink":682,"excerpt":677,"lastModifyTime":683,"phase":116},"\u002Farchives\u002Fgolang-closure-function","2026-09-13T05:18:20.909737Z",[685],{"metadata":686,"spec":687,"status":688,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":690,"spec":693,"status":702,"categories":705},{"name":691,"creationTimestamp":692},"post-golang-break-f71be769","2026-09-12T08:28:47.631962Z",{"title":694,"slug":695,"cover":696,"excerpt":697,"publishTime":699,"categories":700,"tags":701,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言 break 语句（跳出循环）","golang-break","\u002Fupload\u002F164871018843750.png",{"raw":698,"autoGenerate":93},"在 Go 语言中，break 语句可以结束 for、switch 和 select 代码块。通过 break 语句搭配标签可以跳出代码块，如跳出 for 、switch 和 select 代码块。下面的代码演示了如何通过 break 跳出多层循环，代码如下 ...\r\n","2026-09-12T08:28:47.687071Z",[87],[],{"permalink":703,"excerpt":698,"lastModifyTime":704,"phase":116},"\u002Farchives\u002Fgolang-break","2026-09-13T05:18:21.755986Z",[706],{"metadata":707,"spec":708,"status":709,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":711,"spec":714,"status":723,"categories":726},{"name":712,"creationTimestamp":713},"post-golang-array-5f3b0020","2026-09-12T08:28:47.022107Z",{"title":715,"slug":716,"cover":717,"excerpt":718,"publishTime":720,"categories":721,"tags":722,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言数组_定义_初始化_遍历（超详细）","golang-array","\u002Fupload\u002F164870968761852.png",{"raw":719,"autoGenerate":93},"数组是一段固定长度的连续内存区域。在 Go 语言中，数组一旦声明，那么大小就确定了。我们可以修改数组成员，但是不能改变大小。定义数组的格式如下： var 数组变量名 [元素数量]T，下面这段代码中，定义一个变量为 arr, 成员类型为字符串, 大小为 3 的数组 ...\r\n","2026-09-12T08:28:47.076276Z",[87],[],{"permalink":724,"excerpt":719,"lastModifyTime":725,"phase":116},"\u002Farchives\u002Fgolang-array","2026-09-13T05:18:22.626736Z",[727],{"metadata":728,"spec":729,"status":730,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":732,"spec":735,"status":744,"categories":747},{"name":733,"creationTimestamp":734},"post-golang-anonymous-function-cd5ab702","2026-09-12T08:28:46.418365Z",{"title":736,"slug":737,"cover":738,"excerpt":739,"publishTime":741,"categories":742,"tags":743,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言匿名函数_回调函数","golang-anonymous-function","\u002Fupload\u002F164871043722612.png",{"raw":740,"autoGenerate":93},"在 Go 语言中，匿名函数是没有名字的函数，只有函数体。匿名函数经常以变量的形式被传递。大部分场景下，匿名函数经常被使用于实现函数回调、闭包等。匿名函数的定义格式如下 ...\r\n","2026-09-12T08:28:46.479749Z",[87],[],{"permalink":745,"excerpt":740,"lastModifyTime":746,"phase":116},"\u002Farchives\u002Fgolang-anonymous-function","2026-09-13T05:18:23.547060Z",[748],{"metadata":749,"spec":750,"status":751,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":753,"spec":756,"status":765,"categories":768},{"name":754,"creationTimestamp":755},"post-goland-download-1ba6aa8a","2026-09-12T08:28:45.798800Z",{"title":757,"slug":758,"cover":759,"excerpt":760,"publishTime":762,"categories":763,"tags":764,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"Go 语言下载","goland-download","\u002Fupload\u002Fcover-72b74cdd302d.png",{"raw":761,"autoGenerate":93},"想要学习 Go 语言, 首先得下载 Go 语言安装包, 可以前往如下两个网站下载, Golang 中国和Go 语言官网，根据自己的系统，选择对应的 Go 语言安装包，点击下载。","2026-09-12T08:28:45.856679Z",[87],[],{"permalink":766,"excerpt":761,"lastModifyTime":767,"phase":116},"\u002Farchives\u002Fgoland-download","2026-09-14T00:21:02.748134Z",[769],{"metadata":770,"spec":771,"status":772,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":774,"spec":777,"status":786,"categories":790},{"name":775,"creationTimestamp":776},"post-go-get-no-response-f76e24e3","2026-09-12T08:28:45.175847Z",{"title":778,"slug":779,"cover":780,"excerpt":781,"publishTime":783,"categories":784,"tags":785,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},"go get 没有反应，下载不了（设置代理一秒解决）","go-get-no-response","\u002Fupload\u002F164871056889028.png",{"raw":782,"autoGenerate":93},"我在安装好 Go 语言 1.15 环境后，想下载 Gin 框架的依赖包，但是发现长时间没有反应，仿佛卡住了一样，下载不下来。可以通过添加国内代理来解决，方法有两种，任选一种即可 ...","2026-09-12T08:28:45.225479Z",[87],[],{"permalink":787,"excerpt":788,"lastModifyTime":789,"phase":116},"\u002Farchives\u002Fgo-get-no-response","安装好 Go 语言 1.15 环境后，想下载 Gin 框架的依赖包，但是发现长时间没有反应，仿佛卡住了一样，下载不下来。可以通过添加国内代理来解决，方法有两种，任选一种即可 ...","2026-09-27T14:43:19.937379Z",[791],{"metadata":792,"spec":793,"status":794,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"metadata":796,"spec":797,"status":801,"categories":802,"content":807},{"name":229,"creationTimestamp":230},{"title":232,"slug":233,"cover":234,"excerpt":798,"publishTime":237,"categories":799,"tags":800,"priority":27,"pinned":93,"deleted":93,"visible":111,"allowComment":112},{"raw":236,"autoGenerate":93},[87],[],{"permalink":241,"excerpt":236,"lastModifyTime":242,"phase":116},[803],{"metadata":804,"spec":805,"status":806,"postCount":96},{"name":87,"creationTimestamp":88},{"displayName":90,"slug":87,"cover":91,"description":92,"priority":27,"hideFromList":93,"preventParentPostCascadeQuery":93},{"permalink":95,"postCount":96,"visiblePostCount":96},{"content":808,"raw":808},"\u003Cp>上面小节，我们学习的 Go 语言数组\u003Cspan>\u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" style=\"color: #aaa; margin-left: 3px;\" aria-hidden=\"true\" focusable=\"false\" x=\"0px\" y=\"0px\" viewBox=\"0 0 100 100\" width=\"15\" height=\"15\" class=\"icon outbound\">\u003Cpath fill=\"currentColor\" d=\"M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z\">\u003C\u002Fpath> \u003Cpolygon fill=\"currentColor\" points=\"45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9\">\u003C\u002Fpolygon>\u003C\u002Fsvg> \u003Cspan class=\"sr-only\">\u003C\u002Fspan>\u003C\u002Fspan>，本小节我们讲讲切片。\u003C\u002Fp>\n\u003Ch2>一、什么是切片\u003C\u002Fh2>\n\u003Cp>切片和数组类似，都是\u003Cstrong>数据集合\u003C\u002Fstrong>。和数组不同的是，\u003Cstrong>切片是一块动态分配大小的连续空间\u003C\u002Fstrong>。它和 Java 语言中的 AarryList 集合类似。\u003C\u002Fp>\n\u003Ch2>二、声明切片\u003C\u002Fh2>\n\u003Cp>切片的声明格式如下:\u003C\u002Fp>\n\u003Cpre>\u003Ccode>var name []T\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cul>\n\u003Cli>\u003Ccode>name\u003C\u002Fcode> 表示切片变量\u003Cspan>\u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" style=\"color: #aaa; margin-left: 3px;\" aria-hidden=\"true\" focusable=\"false\" x=\"0px\" y=\"0px\" viewBox=\"0 0 100 100\" width=\"15\" height=\"15\" class=\"icon outbound\">\u003Cpath fill=\"currentColor\" d=\"M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z\">\u003C\u002Fpath> \u003Cpolygon fill=\"currentColor\" points=\"45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9\">\u003C\u002Fpolygon>\u003C\u002Fsvg> \u003Cspan class=\"sr-only\">\u003C\u002Fspan>\u003C\u002Fspan>名;\u003C\u002Fli>\n\u003Cli>T 表示切片类型。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>下面是示例代码:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\t\u002F\u002F 声明整型切片\n\tvar numList []int\n\n\t\u002F\u002F 声明字符串切片\n\tvar strList []string\n\n\t\u002F\u002F 声明一个空切片, {} 表示已经分配内存，但是切片里面的元素是空的\n\tvar numListEmpty = []int{}\n\n\t\u002F\u002F 输出3个切片\n\tfmt.Println(numList, strList, numListEmpty)\n\n\t\u002F\u002F 输出3个切片大小\n\tfmt.Println(len(numList), len(strList), len(numListEmpty))\n\n\t\u002F\u002F 切片判定是否为空结果\n\tfmt.Println(numList == nil)\n\tfmt.Println(strList == nil)\n\tfmt.Println(numListEmpty == nil)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>代码输出:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">[] [] []\n0 0 0\ntrue\ntrue\nfalse\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>图示：\u003C\u002Fp>\n\u003Cp>\n\u003Cimg src=\"\u002Fupload\u002F164870974706279.png\" loading=\"lazy\" title=\"go语言切片代码示例\" alt=\"go语言切片代码示例\">\u003Cspan class=\"image-caption\">go语言切片代码示例\u003C\u002Fspan>\n\u003C\u002Fp>\n\u003Ch2>三、使用 make() 函数构造切片\u003C\u002Fh2>\n\u003Cp>可以通过 \u003Ccode>make()\u003C\u002Fcode> 函数动态的创建一个切片，格式如下:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">make( []T, size, cap )\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cul>\n\u003Cli>\u003Ccode>T\u003C\u002Fcode> ： 切片中元素的类型；\u003C\u002Fli>\n\u003Cli>\u003Ccode>size\u003C\u002Fcode> ： 表示为这个类型分配多少个元素；\u003C\u002Fli>\n\u003Cli>\u003Ccode>cap\u003C\u002Fcode> : 预分配的元素数量，该值设定后不影响 size, 表示提前分配的空间，设置它主要用于降低动态扩容时，造成的性能问题。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>示例代码如下:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\ta := make([]int, 4)\n\tb := make([]int, 4, 10)\n\n\tfmt.Println(a, b)\n\tfmt.Println(len(a), len(b))\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>代码输出如下:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">[0 0 0 0] [0 0 0 0]\n4 4\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>a 和 b 切片均为大小为 2， 不同的是 b 内存空间预分配了 10 个，但是实际只使用了 2 个元素。\u003C\u002Fp>\n\u003Cp>\u003Ccode>len()\u003C\u002Fcode> 函数计算的是元素的个数，与切片容量无关。\u003C\u002Fp>\n\u003Ch2>四、使用 append() 函数为切片添加元素\u003C\u002Fh2>\n\u003Cp>Go 语言\u003Cspan>\u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" style=\"color: #aaa; margin-left: 3px;\" aria-hidden=\"true\" focusable=\"false\" x=\"0px\" y=\"0px\" viewBox=\"0 0 100 100\" width=\"15\" height=\"15\" class=\"icon outbound\">\u003Cpath fill=\"currentColor\" d=\"M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z\">\u003C\u002Fpath> \u003Cpolygon fill=\"currentColor\" points=\"45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9\">\u003C\u002Fpolygon>\u003C\u002Fsvg> \u003Cspan class=\"sr-only\">\u003C\u002Fspan>\u003C\u002Fspan>中的内置函数 \u003Ccode>append()\u003C\u002Fcode> 可以为切片动态添加元素, 示例代码如下：\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\t\u002F\u002F 声明一个字符串类型的切片\n\tvar strList []string\n\n\t\u002F\u002F 循环动态向 strList 切片中添加 20 个元素，并打印相关参数\n\tfor i := 0; i &lt; 10; i++ {\n\t\tline := fmt.Sprintf(&quot;mashangan %d&quot;, i)\n\t\tstrList = append(strList, line)\n\n\t\tfmt.Printf(&quot;len: %d, cap: %d, pointer: %p, content: %s\\n&quot;, len(strList), cap(strList), strList, strList[i])\n\t}\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>代码输出如下:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">len: 1, cap: 1, pointer: 0xc00008e1e0, content: mashangan 0\nlen: 2, cap: 2, pointer: 0xc0000a6040, content: mashangan 1\nlen: 3, cap: 4, pointer: 0xc0000b2040, content: mashangan 2\nlen: 4, cap: 4, pointer: 0xc0000b2040, content: mashangan 3\nlen: 5, cap: 8, pointer: 0xc0000bc000, content: mashangan 4\nlen: 6, cap: 8, pointer: 0xc0000bc000, content: mashangan 5\nlen: 7, cap: 8, pointer: 0xc0000bc000, content: mashangan 6\nlen: 8, cap: 8, pointer: 0xc0000bc000, content: mashangan 7\nlen: 9, cap: 16, pointer: 0xc0000be000, content: mashangan 8\nlen: 10, cap: 16, pointer: 0xc0000be000, content: mashangan 9\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>通过上面的代码输出，会发现 \u003Ccode>len()\u003C\u002Fcode> 并不等于 \u003Ccode>cap\u003C\u002Fcode>。这是因为当切片空间不足以容纳足够多的元素时，\u003Cstrong>切片会自动进行扩容操作\u003C\u002Fstrong>, 扩容规律按切片容量的 2 倍进行扩容，如 1、2、4、8、16 ....\u003C\u002Fp>\n\u003Cblockquote>\n\u003Cp>PS: 扩容一般发生在 \u003Ccode>append()\u003C\u002Fcode> 函数调用时。\u003C\u002Fp>\n\u003C\u002Fblockquote>\n\u003Cp>另外，\u003Ccode>append()\u003C\u002Fcode> 函数除了添加一个元素外，还能一次性添加多个元素:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\tvar strList []string\n\n\t\u002F\u002F 添加一个元素\n\tstrList = append(strList, &quot;mashangan&quot;)\n\n\t\u002F\u002F 添加多个元素\n\tstrList = append(strList, &quot;www&quot;, &quot;mashangan&quot;, &quot;com&quot;)\n\n\t\u002F\u002F 添加切片\n\tlist := []string{&quot;张三&quot;, &quot;教程&quot;}\n  \u002F\u002F list 后面的 ... 表示将 list 整个添加到 strList 切片中\n\tstrList = append(strList, list...)\n\n\tfmt.Println(strList)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>代码输出如下:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">[mashangan www mashangan com 张三 教程]\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2>五、从数组或切片生成新的切片\u003C\u002Fh2>\n\u003Cp>从数组或切片生成新的切片是很常见的操作，格式如下:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">slice [开始位置:结束位置]\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cul>\n\u003Cli>\u003Ccode>slice\u003C\u002Fcode> 表示切片目标；\u003C\u002Fli>\n\u003Cli>开始位置和结束位置对应目标切片的下标。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>从数组中生成切片：\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\tvar arr = [3]int{1, 2, 3}\n\t\n\tfmt.Println(arr, arr[1:2])\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>代码输出如下:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">[1 2 3] [2]\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>[2] 是 \u003Ccode>arr[1:2]\u003C\u002Fcode> 切片操作的结果。注意取出的元素不包括结束位置的元素。\u003C\u002Fp>\n\u003Ch3>5.1 从指定范围中生成切片\u003C\u002Fh3>\n\u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\tvar arr = [20]int{}\n\n\t\u002F\u002F 向数组中添加元素\n\tfor i := 0; i &lt; 20; i++ {\n\t\tarr[i] = i + 1\n\t}\n\n\t\u002F\u002F 指定区间\n\tfmt.Println(arr[8:15])\n\n\t\u002F\u002F 中间到尾部所有元素\n\tfmt.Println(arr[10:])\n\n\t\u002F\u002F 开头到中间所有元素\n\tfmt.Println(arr[:10])\n\n\t\u002F\u002F 切片本身\n\tfmt.Println(arr[:])\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>代码输出:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">[9 10 11 12 13 14 15]\n[11 12 13 14 15 16 17 18 19 20]\n[1 2 3 4 5 6 7 8 9 10]\n[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cul>\n\u003Cli>若不填写结束位置，如 \u003Ccode>arr[10:]\u003C\u002Fcode>， 则表示从下标 10 置到数组的结束位置。\u003C\u002Fli>\n\u003Cli>若不填写开始位置，如 \u003Ccode>arr[:10]\u003C\u002Fcode>，则表示从 0 到下标 10 的位置。\u003C\u002Fli>\n\u003Cli>若开始位置和结束位置都不填写，如 \u003Ccode>arr[:]\u003C\u002Fcode>， 则会生成一个和原有切片一样的切片。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Ch3>5.2 重置切片\u003C\u002Fh3>\n\u003Cp>若把切片的开始位置和结束位置都设置为 0， 则会生成一个空的切片：\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\tvar arr = [20]int{}\n\n\t\u002F\u002F 向数组中添加元素\n\tfor i := 0; i &lt; 20; i++ {\n\t\tarr[i] = i + 1\n\t}\n\n\tfmt.Println(arr[0:0])\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>代码输出:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">[]\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2>六、复制切片元素到另一个切片\u003C\u002Fh2>\n\u003Cp>Go 语言内置函数 \u003Ccode>copy()\u003C\u002Fcode> 可以将一个切片中的数据复制到另一个切片中，使用格式如下:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">copy( destSlice, srcSlice []T) int\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cul>\n\u003Cli>\u003Ccode>srcSlice\u003C\u002Fcode> 代表源切片；\u003C\u002Fli>\n\u003Cli>\u003Ccode>destSlice\u003C\u002Fcode> 代表目标切片。注意，目标切片必须有足够的空间来装载源切片的元素个数。返回值为整型，表示实际发生复制的元素个数。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>演示代码如下:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\t\u002F\u002F 设置元素数量为 10\n\tconst count = 10\n\n\t\u002F\u002F 源分片\n\tsrcSlice := make([]int, count)\n\n\t\u002F\u002F 给源分片赋值\n\tfor i := 0; i &lt; count; i++ {\n\t\tsrcSlice[i] = i\n\t}\n\n\t\u002F\u002F 目标分片\n\tdestSlice := make([]int, count)\n\n\t\u002F\u002F 将 srcSlice 分片的数据复制到 destSlice 中\n\tcopy(destSlice, srcSlice)\n\n\tfmt.Println(srcSlice)\n\tfmt.Println(destSlice)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>代码输出:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">[0 1 2 3 4 5 6 7 8 9]\n[0 1 2 3 4 5 6 7 8 9]\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>另外，我们还可以复制指定范围的数据:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">\u002F\u002F 将 srcSlice 分片中指定范围的数据复制到 destSlice 中\ncopy(destSlice, srcSlice[4:8])\n\nfmt.Println(srcSlice)\nfmt.Println(destSlice)\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>代码输出:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">[0 1 2 3 4 5 6 7 8 9]\n[4 5 6 7 0 0 0 0 0 0]\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2>七、从切片中删除元素\u003C\u002Fh2>\n\u003Cp>Go 语言中并没有提供特定的函数来删除切片中元素，但是可以利用切片的特性来达到目的：\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\t\u002F\u002F 声明一个字符串类型的切片\n\tarr := []string{&quot;q&quot;, &quot;u&quot;, &quot;a&quot;, &quot;n&quot;, &quot;x&quot;, &quot;i&quot;, &quot;a&quot;, &quot;o&quot;, &quot;h&quot;, &quot;a&quot;}\n\n\t\u002F\u002F 指定删除位置，也就是 u 元素\n\tindex := 1\n\n\t\u002F\u002F 打印删除位置之前和之后的元素， arr[:index] 表示的是被删除元素的前面部分数据，arr[index+1:] 表示的是被删除元素后面的数据\n\tfmt.Println(arr[:index], arr[index+1:])\n\n\t\u002F\u002F 将删除点前后的元素拼接起来\n\tarr = append(arr[:index], arr[index+1:]...)\n\n\tfmt.Println(arr)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>代码输出如下：\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-go\">[q] [a n x i a o h a]\n[q a n x i a o h a]\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>总结：Go 语言中切片删除元素的本质即： 以被删除元素为分界点, 将前后两个部分的内存重新连接起来。\u003C\u002Fp>\n13:T6144,\u003Cp>上面小节，我们学习的 Go 语言数组\u003Cspan>\u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" style=\"color: #aaa; margin-left: 3px;\" aria-hidden=\"true\" focusable=\"false\" x=\"0px\" y=\"0px\" viewBox=\"0 0 100 100\" width=\"15\" height=\"15\" class=\"icon outbound\">\u003Cpath fill=\"currentColor\" d=\"M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z\">\u003C\u002Fpath> \u003Cpolygon fill=\"currentColor\" points=\"45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9\">\u003C\u002Fpolygon>\u003C\u002Fsvg> \u003Cspan class=\"sr-only\">\u003C\u002Fspan>\u003C\u002Fspan>，本小节我们讲讲切片。\u003C\u002Fp>\n\u003Ch2 id=\"toc-0\">一、什么是切片\u003C\u002Fh2>\n\u003Cp>切片和数组类似，都是\u003Cstrong>数据集合\u003C\u002Fstrong>。和数组不同的是，\u003Cstrong>切片是一块动态分配大小的连续空间\u003C\u002Fstrong>。它和 Java 语言中的 AarryList 集合类似。\u003C\u002Fp>\n\u003Ch2 id=\"toc-1\">二、声明切片\u003C\u002Fh2>\n\u003Cp>切片的声明格式如下:\u003C\u002Fp>\n\u003Cdiv class=\"language-css\" data-ext=\"css\" data-code=\"var%20name%20%5B%5DT%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-css\">var name []T\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cul>\n\u003Cli>\u003Ccode>name\u003C\u002Fcode> 表示切片变量\u003Cspan>\u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" style=\"color: #aaa; margin-left: 3px;\" aria-hidden=\"true\" focusable=\"false\" x=\"0px\" y=\"0px\" viewBox=\"0 0 100 100\" width=\"15\" height=\"15\" class=\"icon outbound\">\u003Cpath fill=\"currentColor\" d=\"M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z\">\u003C\u002Fpath> \u003Cpolygon fill=\"currentColor\" points=\"45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9\">\u003C\u002Fpolygon>\u003C\u002Fsvg> \u003Cspan class=\"sr-only\">\u003C\u002Fspan>\u003C\u002Fspan>名;\u003C\u002Fli>\n\u003Cli>T 表示切片类型。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>下面是示例代码:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"package%20main%0A%0Aimport%20%22fmt%22%0A%0Afunc%20main()%20%20%7B%0A%09%2F%2F%20%E5%A3%B0%E6%98%8E%E6%95%B4%E5%9E%8B%E5%88%87%E7%89%87%0A%09var%20numList%20%5B%5Dint%0A%0A%09%2F%2F%20%E5%A3%B0%E6%98%8E%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%88%87%E7%89%87%0A%09var%20strList%20%5B%5Dstring%0A%0A%09%2F%2F%20%E5%A3%B0%E6%98%8E%E4%B8%80%E4%B8%AA%E7%A9%BA%E5%88%87%E7%89%87%2C%20%7B%7D%20%E8%A1%A8%E7%A4%BA%E5%B7%B2%E7%BB%8F%E5%88%86%E9%85%8D%E5%86%85%E5%AD%98%EF%BC%8C%E4%BD%86%E6%98%AF%E5%88%87%E7%89%87%E9%87%8C%E9%9D%A2%E7%9A%84%E5%85%83%E7%B4%A0%E6%98%AF%E7%A9%BA%E7%9A%84%0A%09var%20numListEmpty%20%3D%20%5B%5Dint%7B%7D%0A%0A%09%2F%2F%20%E8%BE%93%E5%87%BA3%E4%B8%AA%E5%88%87%E7%89%87%0A%09fmt.Println(numList%2C%20strList%2C%20numListEmpty)%0A%0A%09%2F%2F%20%E8%BE%93%E5%87%BA3%E4%B8%AA%E5%88%87%E7%89%87%E5%A4%A7%E5%B0%8F%0A%09fmt.Println(len(numList)%2C%20len(strList)%2C%20len(numListEmpty))%0A%0A%09%2F%2F%20%E5%88%87%E7%89%87%E5%88%A4%E5%AE%9A%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%E7%BB%93%E6%9E%9C%0A%09fmt.Println(numList%20%3D%3D%20nil)%0A%09fmt.Println(strList%20%3D%3D%20nil)%0A%09fmt.Println(numListEmpty%20%3D%3D%20nil)%0A%7D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\t\u002F\u002F 声明整型切片\n\tvar numList []int\n\n\t\u002F\u002F 声明字符串切片\n\tvar strList []string\n\n\t\u002F\u002F 声明一个空切片, {} 表示已经分配内存，但是切片里面的元素是空的\n\tvar numListEmpty = []int{}\n\n\t\u002F\u002F 输出3个切片\n\tfmt.Println(numList, strList, numListEmpty)\n\n\t\u002F\u002F 输出3个切片大小\n\tfmt.Println(len(numList), len(strList), len(numListEmpty))\n\n\t\u002F\u002F 切片判定是否为空结果\n\tfmt.Println(numList == nil)\n\tfmt.Println(strList == nil)\n\tfmt.Println(numListEmpty == nil)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>代码输出:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"%5B%5D%20%5B%5D%20%5B%5D%0A0%200%200%0Atrue%0Atrue%0Afalse%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">[] [] []\n0 0 0\ntrue\ntrue\nfalse\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>图示：\u003C\u002Fp>\n\u003Cp>\n\u003Cdiv class=\"img-loading\">\u003Cimg src=\"\u002Fupload\u002F164870974706279.png\" loading=\"lazy\" title=\"go语言切片代码示例\" alt=\"go语言切片代码示例\">\u003C\u002Fdiv>\u003Cspan class=\"image-caption\">go语言切片代码示例\u003C\u002Fspan>\n\u003C\u002Fp>\n\u003Ch2 id=\"toc-2\">三、使用 make() 函数构造切片\u003C\u002Fh2>\n\u003Cp>可以通过 \u003Ccode>make()\u003C\u002Fcode> 函数动态的创建一个切片，格式如下:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"make(%20%5B%5DT%2C%20size%2C%20cap%20)%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">make( []T, size, cap )\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cul>\n\u003Cli>\u003Ccode>T\u003C\u002Fcode> ： 切片中元素的类型；\u003C\u002Fli>\n\u003Cli>\u003Ccode>size\u003C\u002Fcode> ： 表示为这个类型分配多少个元素；\u003C\u002Fli>\n\u003Cli>\u003Ccode>cap\u003C\u002Fcode> : 预分配的元素数量，该值设定后不影响 size, 表示提前分配的空间，设置它主要用于降低动态扩容时，造成的性能问题。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>示例代码如下:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"package%20main%0A%0Aimport%20%22fmt%22%0A%0Afunc%20main()%20%20%7B%0A%09a%20%3A%3D%20make(%5B%5Dint%2C%204)%0A%09b%20%3A%3D%20make(%5B%5Dint%2C%204%2C%2010)%0A%0A%09fmt.Println(a%2C%20b)%0A%09fmt.Println(len(a)%2C%20len(b))%0A%7D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\ta := make([]int, 4)\n\tb := make([]int, 4, 10)\n\n\tfmt.Println(a, b)\n\tfmt.Println(len(a), len(b))\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>代码输出如下:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"%5B0%200%200%200%5D%20%5B0%200%200%200%5D%0A4%204%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">[0 0 0 0] [0 0 0 0]\n4 4\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>a 和 b 切片均为大小为 2， 不同的是 b 内存空间预分配了 10 个，但是实际只使用了 2 个元素。\u003C\u002Fp>\n\u003Cp>\u003Ccode>len()\u003C\u002Fcode> 函数计算的是元素的个数，与切片容量无关。\u003C\u002Fp>\n\u003Ch2 id=\"toc-3\">四、使用 append() 函数为切片添加元素\u003C\u002Fh2>\n\u003Cp>Go 语言\u003Cspan>\u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" style=\"color: #aaa; margin-left: 3px;\" aria-hidden=\"true\" focusable=\"false\" x=\"0px\" y=\"0px\" viewBox=\"0 0 100 100\" width=\"15\" height=\"15\" class=\"icon outbound\">\u003Cpath fill=\"currentColor\" d=\"M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z\">\u003C\u002Fpath> \u003Cpolygon fill=\"currentColor\" points=\"45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9\">\u003C\u002Fpolygon>\u003C\u002Fsvg> \u003Cspan class=\"sr-only\">\u003C\u002Fspan>\u003C\u002Fspan>中的内置函数 \u003Ccode>append()\u003C\u002Fcode> 可以为切片动态添加元素, 示例代码如下：\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"package%20main%0A%0Aimport%20%22fmt%22%0A%0Afunc%20main()%20%20%7B%0A%09%2F%2F%20%E5%A3%B0%E6%98%8E%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%88%87%E7%89%87%0A%09var%20strList%20%5B%5Dstring%0A%0A%09%2F%2F%20%E5%BE%AA%E7%8E%AF%E5%8A%A8%E6%80%81%E5%90%91%20strList%20%E5%88%87%E7%89%87%E4%B8%AD%E6%B7%BB%E5%8A%A0%2020%20%E4%B8%AA%E5%85%83%E7%B4%A0%EF%BC%8C%E5%B9%B6%E6%89%93%E5%8D%B0%E7%9B%B8%E5%85%B3%E5%8F%82%E6%95%B0%0A%09for%20i%20%3A%3D%200%3B%20i%20%3C%2010%3B%20i%2B%2B%20%7B%0A%09%09line%20%3A%3D%20fmt.Sprintf(%22mashangan%20%25d%22%2C%20i)%0A%09%09strList%20%3D%20append(strList%2C%20line)%0A%0A%09%09fmt.Printf(%22len%3A%20%25d%2C%20cap%3A%20%25d%2C%20pointer%3A%20%25p%2C%20content%3A%20%25s%5Cn%22%2C%20len(strList)%2C%20cap(strList)%2C%20strList%2C%20strList%5Bi%5D)%0A%09%7D%0A%7D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\t\u002F\u002F 声明一个字符串类型的切片\n\tvar strList []string\n\n\t\u002F\u002F 循环动态向 strList 切片中添加 20 个元素，并打印相关参数\n\tfor i := 0; i &lt; 10; i++ {\n\t\tline := fmt.Sprintf(&quot;mashangan %d&quot;, i)\n\t\tstrList = append(strList, line)\n\n\t\tfmt.Printf(&quot;len: %d, cap: %d, pointer: %p, content: %s\\n&quot;, len(strList), cap(strList), strList, strList[i])\n\t}\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>代码输出如下:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"len%3A%201%2C%20cap%3A%201%2C%20pointer%3A%200xc00008e1e0%2C%20content%3A%20mashangan%200%0Alen%3A%202%2C%20cap%3A%202%2C%20pointer%3A%200xc0000a6040%2C%20content%3A%20mashangan%201%0Alen%3A%203%2C%20cap%3A%204%2C%20pointer%3A%200xc0000b2040%2C%20content%3A%20mashangan%202%0Alen%3A%204%2C%20cap%3A%204%2C%20pointer%3A%200xc0000b2040%2C%20content%3A%20mashangan%203%0Alen%3A%205%2C%20cap%3A%208%2C%20pointer%3A%200xc0000bc000%2C%20content%3A%20mashangan%204%0Alen%3A%206%2C%20cap%3A%208%2C%20pointer%3A%200xc0000bc000%2C%20content%3A%20mashangan%205%0Alen%3A%207%2C%20cap%3A%208%2C%20pointer%3A%200xc0000bc000%2C%20content%3A%20mashangan%206%0Alen%3A%208%2C%20cap%3A%208%2C%20pointer%3A%200xc0000bc000%2C%20content%3A%20mashangan%207%0Alen%3A%209%2C%20cap%3A%2016%2C%20pointer%3A%200xc0000be000%2C%20content%3A%20mashangan%208%0Alen%3A%2010%2C%20cap%3A%2016%2C%20pointer%3A%200xc0000be000%2C%20content%3A%20mashangan%209%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">len: 1, cap: 1, pointer: 0xc00008e1e0, content: mashangan 0\nlen: 2, cap: 2, pointer: 0xc0000a6040, content: mashangan 1\nlen: 3, cap: 4, pointer: 0xc0000b2040, content: mashangan 2\nlen: 4, cap: 4, pointer: 0xc0000b2040, content: mashangan 3\nlen: 5, cap: 8, pointer: 0xc0000bc000, content: mashangan 4\nlen: 6, cap: 8, pointer: 0xc0000bc000, content: mashangan 5\nlen: 7, cap: 8, pointer: 0xc0000bc000, content: mashangan 6\nlen: 8, cap: 8, pointer: 0xc0000bc000, content: mashangan 7\nlen: 9, cap: 16, pointer: 0xc0000be000, content: mashangan 8\nlen: 10, cap: 16, pointer: 0xc0000be000, content: mashangan 9\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>通过上面的代码输出，会发现 \u003Ccode>len()\u003C\u002Fcode> 并不等于 \u003Ccode>cap\u003C\u002Fcode>。这是因为当切片空间不足以容纳足够多的元素时，\u003Cstrong>切片会自动进行扩容操作\u003C\u002Fstrong>, 扩容规律按切片容量的 2 倍进行扩容，如 1、2、4、8、16 ....\u003C\u002Fp>\n\u003Cblockquote>\n\u003Cp>PS: 扩容一般发生在 \u003Ccode>append()\u003C\u002Fcode> 函数调用时。\u003C\u002Fp>\n\u003C\u002Fblockquote>\n\u003Cp>另外，\u003Ccode>append()\u003C\u002Fcode> 函数除了添加一个元素外，还能一次性添加多个元素:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"package%20main%0A%0Aimport%20%22fmt%22%0A%0Afunc%20main()%20%20%7B%0A%09var%20strList%20%5B%5Dstring%0A%0A%09%2F%2F%20%E6%B7%BB%E5%8A%A0%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0%0A%09strList%20%3D%20append(strList%2C%20%22mashangan%22)%0A%0A%09%2F%2F%20%E6%B7%BB%E5%8A%A0%E5%A4%9A%E4%B8%AA%E5%85%83%E7%B4%A0%0A%09strList%20%3D%20append(strList%2C%20%22www%22%2C%20%22mashangan%22%2C%20%22com%22)%0A%0A%09%2F%2F%20%E6%B7%BB%E5%8A%A0%E5%88%87%E7%89%87%0A%09list%20%3A%3D%20%5B%5Dstring%7B%22%E5%BC%A0%E4%B8%89%22%2C%20%22%E6%95%99%E7%A8%8B%22%7D%0A%20%20%2F%2F%20list%20%E5%90%8E%E9%9D%A2%E7%9A%84%20...%20%E8%A1%A8%E7%A4%BA%E5%B0%86%20list%20%E6%95%B4%E4%B8%AA%E6%B7%BB%E5%8A%A0%E5%88%B0%20strList%20%E5%88%87%E7%89%87%E4%B8%AD%0A%09strList%20%3D%20append(strList%2C%20list...)%0A%0A%09fmt.Println(strList)%0A%7D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\tvar strList []string\n\n\t\u002F\u002F 添加一个元素\n\tstrList = append(strList, &quot;mashangan&quot;)\n\n\t\u002F\u002F 添加多个元素\n\tstrList = append(strList, &quot;www&quot;, &quot;mashangan&quot;, &quot;com&quot;)\n\n\t\u002F\u002F 添加切片\n\tlist := []string{&quot;张三&quot;, &quot;教程&quot;}\n  \u002F\u002F list 后面的 ... 表示将 list 整个添加到 strList 切片中\n\tstrList = append(strList, list...)\n\n\tfmt.Println(strList)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>代码输出如下:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"%5Bmashangan%20www%20mashangan%20com%20%E5%BC%A0%E4%B8%89%20%E6%95%99%E7%A8%8B%5D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">[mashangan www mashangan com 张三 教程]\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Ch2 id=\"toc-4\">五、从数组或切片生成新的切片\u003C\u002Fh2>\n\u003Cp>从数组或切片生成新的切片是很常见的操作，格式如下:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"slice%20%5B%E5%BC%80%E5%A7%8B%E4%BD%8D%E7%BD%AE%3A%E7%BB%93%E6%9D%9F%E4%BD%8D%E7%BD%AE%5D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">slice [开始位置:结束位置]\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cul>\n\u003Cli>\u003Ccode>slice\u003C\u002Fcode> 表示切片目标；\u003C\u002Fli>\n\u003Cli>开始位置和结束位置对应目标切片的下标。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>从数组中生成切片：\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"package%20main%0A%0Aimport%20%22fmt%22%0A%0Afunc%20main()%20%20%7B%0A%09var%20arr%20%3D%20%5B3%5Dint%7B1%2C%202%2C%203%7D%0A%09%0A%09fmt.Println(arr%2C%20arr%5B1%3A2%5D)%0A%7D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\tvar arr = [3]int{1, 2, 3}\n\t\n\tfmt.Println(arr, arr[1:2])\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>代码输出如下:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"%5B1%202%203%5D%20%5B2%5D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">[1 2 3] [2]\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>[2] 是 \u003Ccode>arr[1:2]\u003C\u002Fcode> 切片操作的结果。注意取出的元素不包括结束位置的元素。\u003C\u002Fp>\n\u003Ch3 id=\"toc-5\">5.1 从指定范围中生成切片\u003C\u002Fh3>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"package%20main%0A%0Aimport%20%22fmt%22%0A%0Afunc%20main()%20%20%7B%0A%09var%20arr%20%3D%20%5B20%5Dint%7B%7D%0A%0A%09%2F%2F%20%E5%90%91%E6%95%B0%E7%BB%84%E4%B8%AD%E6%B7%BB%E5%8A%A0%E5%85%83%E7%B4%A0%0A%09for%20i%20%3A%3D%200%3B%20i%20%3C%2020%3B%20i%2B%2B%20%7B%0A%09%09arr%5Bi%5D%20%3D%20i%20%2B%201%0A%09%7D%0A%0A%09%2F%2F%20%E6%8C%87%E5%AE%9A%E5%8C%BA%E9%97%B4%0A%09fmt.Println(arr%5B8%3A15%5D)%0A%0A%09%2F%2F%20%E4%B8%AD%E9%97%B4%E5%88%B0%E5%B0%BE%E9%83%A8%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0%0A%09fmt.Println(arr%5B10%3A%5D)%0A%0A%09%2F%2F%20%E5%BC%80%E5%A4%B4%E5%88%B0%E4%B8%AD%E9%97%B4%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0%0A%09fmt.Println(arr%5B%3A10%5D)%0A%0A%09%2F%2F%20%E5%88%87%E7%89%87%E6%9C%AC%E8%BA%AB%0A%09fmt.Println(arr%5B%3A%5D)%0A%7D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\tvar arr = [20]int{}\n\n\t\u002F\u002F 向数组中添加元素\n\tfor i := 0; i &lt; 20; i++ {\n\t\tarr[i] = i + 1\n\t}\n\n\t\u002F\u002F 指定区间\n\tfmt.Println(arr[8:15])\n\n\t\u002F\u002F 中间到尾部所有元素\n\tfmt.Println(arr[10:])\n\n\t\u002F\u002F 开头到中间所有元素\n\tfmt.Println(arr[:10])\n\n\t\u002F\u002F 切片本身\n\tfmt.Println(arr[:])\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>代码输出:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"%5B9%2010%2011%2012%2013%2014%2015%5D%0A%5B11%2012%2013%2014%2015%2016%2017%2018%2019%2020%5D%0A%5B1%202%203%204%205%206%207%208%209%2010%5D%0A%5B1%202%203%204%205%206%207%208%209%2010%2011%2012%2013%2014%2015%2016%2017%2018%2019%2020%5D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">[9 10 11 12 13 14 15]\n[11 12 13 14 15 16 17 18 19 20]\n[1 2 3 4 5 6 7 8 9 10]\n[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cul>\n\u003Cli>若不填写结束位置，如 \u003Ccode>arr[10:]\u003C\u002Fcode>， 则表示从下标 10 置到数组的结束位置。\u003C\u002Fli>\n\u003Cli>若不填写开始位置，如 \u003Ccode>arr[:10]\u003C\u002Fcode>，则表示从 0 到下标 10 的位置。\u003C\u002Fli>\n\u003Cli>若开始位置和结束位置都不填写，如 \u003Ccode>arr[:]\u003C\u002Fcode>， 则会生成一个和原有切片一样的切片。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Ch3 id=\"toc-6\">5.2 重置切片\u003C\u002Fh3>\n\u003Cp>若把切片的开始位置和结束位置都设置为 0， 则会生成一个空的切片：\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"package%20main%0A%0Aimport%20%22fmt%22%0A%0Afunc%20main()%20%20%7B%0A%09var%20arr%20%3D%20%5B20%5Dint%7B%7D%0A%0A%09%2F%2F%20%E5%90%91%E6%95%B0%E7%BB%84%E4%B8%AD%E6%B7%BB%E5%8A%A0%E5%85%83%E7%B4%A0%0A%09for%20i%20%3A%3D%200%3B%20i%20%3C%2020%3B%20i%2B%2B%20%7B%0A%09%09arr%5Bi%5D%20%3D%20i%20%2B%201%0A%09%7D%0A%0A%09fmt.Println(arr%5B0%3A0%5D)%0A%7D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\tvar arr = [20]int{}\n\n\t\u002F\u002F 向数组中添加元素\n\tfor i := 0; i &lt; 20; i++ {\n\t\tarr[i] = i + 1\n\t}\n\n\tfmt.Println(arr[0:0])\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>代码输出:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"%5B%5D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">[]\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Ch2 id=\"toc-7\">六、复制切片元素到另一个切片\u003C\u002Fh2>\n\u003Cp>Go 语言内置函数 \u003Ccode>copy()\u003C\u002Fcode> 可以将一个切片中的数据复制到另一个切片中，使用格式如下:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"copy(%20destSlice%2C%20srcSlice%20%5B%5DT)%20int%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">copy( destSlice, srcSlice []T) int\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cul>\n\u003Cli>\u003Ccode>srcSlice\u003C\u002Fcode> 代表源切片；\u003C\u002Fli>\n\u003Cli>\u003Ccode>destSlice\u003C\u002Fcode> 代表目标切片。注意，目标切片必须有足够的空间来装载源切片的元素个数。返回值为整型，表示实际发生复制的元素个数。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>演示代码如下:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"package%20main%0A%0Aimport%20%22fmt%22%0A%0Afunc%20main()%20%20%7B%0A%09%2F%2F%20%E8%AE%BE%E7%BD%AE%E5%85%83%E7%B4%A0%E6%95%B0%E9%87%8F%E4%B8%BA%2010%0A%09const%20count%20%3D%2010%0A%0A%09%2F%2F%20%E6%BA%90%E5%88%86%E7%89%87%0A%09srcSlice%20%3A%3D%20make(%5B%5Dint%2C%20count)%0A%0A%09%2F%2F%20%E7%BB%99%E6%BA%90%E5%88%86%E7%89%87%E8%B5%8B%E5%80%BC%0A%09for%20i%20%3A%3D%200%3B%20i%20%3C%20count%3B%20i%2B%2B%20%7B%0A%09%09srcSlice%5Bi%5D%20%3D%20i%0A%09%7D%0A%0A%09%2F%2F%20%E7%9B%AE%E6%A0%87%E5%88%86%E7%89%87%0A%09destSlice%20%3A%3D%20make(%5B%5Dint%2C%20count)%0A%0A%09%2F%2F%20%E5%B0%86%20srcSlice%20%E5%88%86%E7%89%87%E7%9A%84%E6%95%B0%E6%8D%AE%E5%A4%8D%E5%88%B6%E5%88%B0%20destSlice%20%E4%B8%AD%0A%09copy(destSlice%2C%20srcSlice)%0A%0A%09fmt.Println(srcSlice)%0A%09fmt.Println(destSlice)%0A%7D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\t\u002F\u002F 设置元素数量为 10\n\tconst count = 10\n\n\t\u002F\u002F 源分片\n\tsrcSlice := make([]int, count)\n\n\t\u002F\u002F 给源分片赋值\n\tfor i := 0; i &lt; count; i++ {\n\t\tsrcSlice[i] = i\n\t}\n\n\t\u002F\u002F 目标分片\n\tdestSlice := make([]int, count)\n\n\t\u002F\u002F 将 srcSlice 分片的数据复制到 destSlice 中\n\tcopy(destSlice, srcSlice)\n\n\tfmt.Println(srcSlice)\n\tfmt.Println(destSlice)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>代码输出:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"%5B0%201%202%203%204%205%206%207%208%209%5D%0A%5B0%201%202%203%204%205%206%207%208%209%5D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">[0 1 2 3 4 5 6 7 8 9]\n[0 1 2 3 4 5 6 7 8 9]\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>另外，我们还可以复制指定范围的数据:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"%2F%2F%20%E5%B0%86%20srcSlice%20%E5%88%86%E7%89%87%E4%B8%AD%E6%8C%87%E5%AE%9A%E8%8C%83%E5%9B%B4%E7%9A%84%E6%95%B0%E6%8D%AE%E5%A4%8D%E5%88%B6%E5%88%B0%20destSlice%20%E4%B8%AD%0Acopy(destSlice%2C%20srcSlice%5B4%3A8%5D)%0A%0Afmt.Println(srcSlice)%0Afmt.Println(destSlice)%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">\u002F\u002F 将 srcSlice 分片中指定范围的数据复制到 destSlice 中\ncopy(destSlice, srcSlice[4:8])\n\nfmt.Println(srcSlice)\nfmt.Println(destSlice)\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>代码输出:\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"%5B0%201%202%203%204%205%206%207%208%209%5D%0A%5B4%205%206%207%200%200%200%200%200%200%5D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">[0 1 2 3 4 5 6 7 8 9]\n[4 5 6 7 0 0 0 0 0 0]\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Ch2 id=\"toc-8\">七、从切片中删除元素\u003C\u002Fh2>\n\u003Cp>Go 语言中并没有提供特定的函数来删除切片中元素，但是可以利用切片的特性来达到目的：\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"package%20main%0A%0Aimport%20%22fmt%22%0A%0Afunc%20main()%20%20%7B%0A%09%2F%2F%20%E5%A3%B0%E6%98%8E%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%88%87%E7%89%87%0A%09arr%20%3A%3D%20%5B%5Dstring%7B%22q%22%2C%20%22u%22%2C%20%22a%22%2C%20%22n%22%2C%20%22x%22%2C%20%22i%22%2C%20%22a%22%2C%20%22o%22%2C%20%22h%22%2C%20%22a%22%7D%0A%0A%09%2F%2F%20%E6%8C%87%E5%AE%9A%E5%88%A0%E9%99%A4%E4%BD%8D%E7%BD%AE%EF%BC%8C%E4%B9%9F%E5%B0%B1%E6%98%AF%20u%20%E5%85%83%E7%B4%A0%0A%09index%20%3A%3D%201%0A%0A%09%2F%2F%20%E6%89%93%E5%8D%B0%E5%88%A0%E9%99%A4%E4%BD%8D%E7%BD%AE%E4%B9%8B%E5%89%8D%E5%92%8C%E4%B9%8B%E5%90%8E%E7%9A%84%E5%85%83%E7%B4%A0%EF%BC%8C%20arr%5B%3Aindex%5D%20%E8%A1%A8%E7%A4%BA%E7%9A%84%E6%98%AF%E8%A2%AB%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0%E7%9A%84%E5%89%8D%E9%9D%A2%E9%83%A8%E5%88%86%E6%95%B0%E6%8D%AE%EF%BC%8Carr%5Bindex%2B1%3A%5D%20%E8%A1%A8%E7%A4%BA%E7%9A%84%E6%98%AF%E8%A2%AB%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0%E5%90%8E%E9%9D%A2%E7%9A%84%E6%95%B0%E6%8D%AE%0A%09fmt.Println(arr%5B%3Aindex%5D%2C%20arr%5Bindex%2B1%3A%5D)%0A%0A%09%2F%2F%20%E5%B0%86%E5%88%A0%E9%99%A4%E7%82%B9%E5%89%8D%E5%90%8E%E7%9A%84%E5%85%83%E7%B4%A0%E6%8B%BC%E6%8E%A5%E8%B5%B7%E6%9D%A5%0A%09arr%20%3D%20append(arr%5B%3Aindex%5D%2C%20arr%5Bindex%2B1%3A%5D...)%0A%0A%09fmt.Println(arr)%0A%7D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main()  {\n\t\u002F\u002F 声明一个字符串类型的切片\n\tarr := []string{&quot;q&quot;, &quot;u&quot;, &quot;a&quot;, &quot;n&quot;, &quot;x&quot;, &quot;i&quot;, &quot;a&quot;, &quot;o&quot;, &quot;h&quot;, &quot;a&quot;}\n\n\t\u002F\u002F 指定删除位置，也就是 u 元素\n\tindex := 1\n\n\t\u002F\u002F 打印删除位置之前和之后的元素， arr[:index] 表示的是被删除元素的前面部分数据，arr[index+1:] 表示的是被删除元素后面的数据\n\tfmt.Println(arr[:index], arr[index+1:])\n\n\t\u002F\u002F 将删除点前后的元素拼接起来\n\tarr = append(arr[:index], arr[index+1:]...)\n\n\tfmt.Println(arr)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>代码输出如下：\u003C\u002Fp>\n\u003Cdiv class=\"language-go\" data-ext=\"go\" data-code=\"%5Bq%5D%20%5Ba%20n%20x%20i%20a%20o%20h%20a%5D%0A%5Bq%20a%20n%20x%20i%20a%20o%20h%20a%5D%0A\">\n        \u003Cbutton class=\"vp-copy-code-button\" data-copy data-copied=\"已复制\" aria-label=\"复制代码\">\u003C\u002Fbutton>\n        \u003Cpre>\u003Ccode class=\"language-go\">[q] [a n x i a o h a]\n[q a n x i a o h a]\n\u003C\u002Fcode>\u003C\u002Fpre>\n      \u003C\u002Fdiv>\n\u003Cp>总结：Go 语言中切片删除元素的本质即： 以被删除元素为分界点, 将前后两个部分的内存重新连接起来。\u003C\u002Fp>"]