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

建设一个网站的一般过程志鸿优化设计官网

建设一个网站的一般过程,志鸿优化设计官网,石家庄网站编辑,禹城网站建设一、切片基础 1、切片的定义 切片(Slice)是一个拥有相同类型元素的可变长度的序列它是基于数组类型做的一层封装它非常灵活,支持自动扩容切片是一个引用类型,它的内部结构包含地址、长度和容量声明切片类型的基本语法如下&#…

一、切片基础

1、切片的定义
  • 切片(Slice)是一个拥有相同类型元素的可变长度的序列
  • 它是基于数组类型做的一层封装
  • 它非常灵活,支持自动扩容
  • 切片是一个引用类型,它的内部结构包含地址、长度和容量
  • 声明切片类型的基本语法如下:
  • var name []T
  • name:表示变量名
  • T:表示切片中的元素类型
package mainimport "fmt"func main() {//切片是引用类型,不支持直接比较,只能和 nil 比较var a []string    //声明一个字符串切片fmt.PrintIn(a)    //[]fmt.PrintIn(a == nil)    //truevar b = []int{}    //声明一个整型切片并初始化fmt.PrintIn(b)    //[]fmt.PrintIn(b==nil)    //falsevar c = []bool{false,true}fmt.PrintIn(c)    //[false true]fmt.PrintIn(c == nil)    //false
}
  • 切片之间是不能比较的,我们不能使用--操作符来判断两个切片是否含有全部相等元素
  • 切片唯一合法的比较操作是和 nil 比较。一个 nil 值的切片并没有底层数组,一个 nil 值的切片的长度和容量都是 0
  • 但是我们不能说一个长度和容量都是 0 的切片一定是 nil
2、关于 nil 的认识
  • 当你声明了一个变量,但却还并没有赋值是,golang 中自动给你的变量赋值一个默认零值
  • 这是每种类型对应的零值
bool->false
numbers->0
string->""
pointers->nil
slices->nul
maps->nil
channels->nil
functions->nil
interfaces->nil
3、切片的本质
  • 切片的本质就是对底层数组的封装,他包含了三个信息:底层数组的指针、切片的长度(len)和切片的容量(cap)
  • 举个例子,现在有一个数组 a:=[8]int{0,1,2,3,4,5,6,7},切片s1:=a[:5],相应示意图如下。

  • 切片s2:=a[3:6],相应示意图如下

4、切片的扩容策略
  •  首先判断,如果新申请容量(cap)大于 2 倍的就容量(old.cap),最终容量(newcap)就是新申请的容量(cap)
  • 否则判断,如果旧切片的长度小于 1024,则最终容量(newcap)就是旧容量(old.cap)的两倍,即(newcap=doublecap)
  • 否则判断,如果旧切片长度大于等于 1024,则最终容量(newcap)从旧容量(old.cap)开始循环怎么感觉原来的1/4,即(newcap=old.cap,for{newcap+= newcap/4})直到最终容量newcap)大于等于新申请的容量(cap),即(newcap >= cap)
  • 如果最终容量(cap)计算值溢出,则最终容量 cap 就是新申请容量(cap)
5、切片的长度和容量
  • 切片拥有自己的长度和容量,我们可以通过使用内置的**len()**函数求长度,使用内置的**cap()**函数求切片的容量
  • 切片的长度就是它所包含的元素个数
  • 切片的容量是从它的第一个元素开始数,到其底层数组元素末尾的个数
  • 切片 s 的长度和容量可通过表达式 len(s)和 cap(s)来获取
package mainimport "fmt"func main(){s:=[]int{2,3,5,7,11,13}fmt.Printf("长度:%v 容量%v\n",len(s),cap(s))    //长度:6 容量 6c := s[:2]fmt.PrintIn(c)    //[2 3]fmt.Printf("长度:%v 容量 %v\n",len(c),cap(c))    //长度 2 容量 6d :=s[1:3]fmt.PrintIn(d)    //[3 5]fmt.Printf("长度:%v 容量%v",len(d),cap(d))    //长度:2 容量 5
}

二、切片循环

  • 切片的循环遍历和数组的循环遍历是一样的
1、基本遍历
package mainimport "fmt"func main(){var a = []string{"北京","上海","深圳"}for i:=0;i<len(a);i++{fmt.PrintIn(a[i])}
/*
北京
上海
深圳*/
}
2、k,v 遍历
package mainimport "fmt"func main() {var a = []string{"北京", "上海", "深圳"}for index, value := range a {fmt.Println(index, value)}
}
/*
0 北京
1 上海
2 深圳*/

三、定义切片

1、数组定义切片
  • 由于切片的底层就是一个数组,所以我们可以基于数组定义切片
package mainimport   "fmt"func main() {a := [5]int{55,56,57,58,59}    //基于数组定义切片b := a[1:4]fmt.PrintIn(b)    // [56 57 58]fmt.Printf("type of b:%T\n",b)    //type of b:[]intc := b[0:2]fmt.PrintIn(c)    //[56 57]
}
2、make()构造切片
  • 我们上面都是基于数组来来创建的切片,如果需要动态的创建一个切片,我们就需要使用内置的make()函数
  • 格式如下:make([]T,size,cap)
    • T:切片的元素类型
    • size:切片中元素的数量
    • cap:切片的容量
package mainimport "fmt"func main() {a := make([]int,2,10)fmt.PrintIn(a)    //[0 0]fmt.PrintIn(len(a))    // 2fmt.PrintIn(cap(a))    //10
}
  • 在上面代码a的内部存储空间已经分配了10个,但实际上只用了2个
  • 容量并不会影响当前元素的个数,所以len(a)返回2,cap(a)则返回该切片的容量

四、append()

  • Go语言的聂键函数append()可以为切片动态添加元素,每个切片会指向一个底层数组
  • 这个数组的容量够用就添加新增元素
  • 当底层数组不能容纳新增的元素时,切片就会自动按照一定的策略进行“扩容”,此时该切片指向的底层数组就会更换
  • “扩容”操作往往发送在append()函数调用时,所以我们通常都需要用原变量接收append()函数的返回值
1、append添加
package  mainimport "fmt"func main() {//append()添加元素和切片扩容var numSlice []intfor i :=0;i<10;i++ {numSlice =append(numSlice,i)fmt.Printf("%v len:%d cap:%d ptr:%p\n",numSlice,len(numSlice),cap(numSlice),numSlice)}
}
2、append追加多个
package mainimport "fmt"func main() {var citySlice []stringcitySlice = append(citySlice,"北京")    //追加一个元素citySlice = append(citySlice,"上海","杭州","深圳")    //追加多个元素a := []string{"成都","重庆"}citySlice = append(citySlice,a...)    //追加切片fmt.PrintIn(citySlice)    //[北京 上海 杭州 深圳 成都 重庆]
}
3、切片中删除元素
  • Go语言中并没有删除切片元素的专用方法,我们可以使用切片本身的特性来删除元素
package  mainimport "fmt"func main() {a := int{30,31,32,33,34,35,36,37}a = append(a[:2],a[3:]...)    //要删除索引为2的元素fmt.PrintIn(a)    //[30 31 33 34 35 36 37]
}
4、切片合并
package mainimport "fmt"func main() {arr1 :=[]int{2,7,1}arr2 :=[]int{5,9,3}fmt.PrintIn(arr2,arr1)arr1 = append(arr1,arr2...)fmt.PrintIn(arr1)    //[2 7 1 5 9 3]
}

五、copy()

1、引用问题
package mainimport "fmt"func main() {a := []int{1,2,3,4,5}b :=afmt.PrintIn(a)    //[1 2 3 4 5]fmt.PrintIn(b)    //[1 2 3 4 5]b[0] = 1000fmt.PrintIn(a)    //[1000 2 3 4 5]fmt.Println(b)   //[1000 2 3 4 5]
}
2、copy()函数
  • Go语言内建的copy()函数可以迅速地将一个切片的数据复制到另外一个切片空间中
  • copy()函数的使用格式如下:copy(destSlice,srcSlice [] T)
  • 其中
    • srcSlice:数据来源切片
    • destSlice:目标切片
package mainimport "fmt"func main() {a := []int{1,2,3,4,5}c := make([]int,5,5)    //[0 0 0 0 0]copy(c,a)    //使用copy函数将切片a中的元素复制到切片cfmt.Println(a)     //[1 2 3 4 5]fmt.Println(c)     //[1 2 3 4 5]c[0] = 1000fmt.Println(a)   //[1 2 3 4 5]fmt.Println(c)   //[1000 2 3 4 5]
}

六、sort()

1、正序排序
  • 对于int、float64和string数组或是切片的排序
  • go分别提供了sort.Ints()、sort.Float64s()和sort.Strings()函数,默认都是从小到大排序
pacakge mainimport {"fmt""sort"
}func main() {intList := []int{2, 4, 3, 5, 7, 6, 9, 8, 1, 0}sort.Ints(intList)fmt.Println(intList)   // [0 1 2 3 4 5 6 7 8 9]stringList := []string{"a", "c", "b", "z", "x", "w", "y", "d", "f", "i"}sort.Strings(stringList)fmt.Println(stringList)   // [a b c d f i w x y z]
}

2、sort降序排序

  • Golang的sort 包 可 以 使 用 sort.Reverse(slice) 来 调 换slice.Interface.Less
  • 也就是比较函数,所以, int 、 float64 和 string的逆序排序函数可以这么写
package main
import ("fmt""sort"
)
func main() {intList := []int{2, 4, 3, 5, 7, 6, 9, 8, 1, 0}sort.Sort(sort.Reverse(sort.IntSlice(intList)))fmt.Println(intList)   // [9 8 7 6 5 4 3 2 1 0]stringList := []string{"a", "c", "b", "z", "x", "w", "y", "d", "f", "i"}sort.Sort(sort.Reverse(sort.StringSlice(stringList)))fmt.Println(stringList)   // [z y x w i f d c b a]
}

文章转载自:
http://untold.stph.cn
http://attagirl.stph.cn
http://hypersecretion.stph.cn
http://nerveless.stph.cn
http://hypnone.stph.cn
http://scarp.stph.cn
http://straucht.stph.cn
http://kondo.stph.cn
http://leu.stph.cn
http://otologist.stph.cn
http://padova.stph.cn
http://coz.stph.cn
http://sacrilegiously.stph.cn
http://hepatotomy.stph.cn
http://gymnogenous.stph.cn
http://adhibition.stph.cn
http://brush.stph.cn
http://bakeshop.stph.cn
http://veritas.stph.cn
http://bandgap.stph.cn
http://logger.stph.cn
http://assimilatory.stph.cn
http://detoxicator.stph.cn
http://butte.stph.cn
http://judea.stph.cn
http://armomancy.stph.cn
http://triphammer.stph.cn
http://polymerization.stph.cn
http://inconsonance.stph.cn
http://countersubject.stph.cn
http://interfirm.stph.cn
http://ancylostomiasis.stph.cn
http://traumatism.stph.cn
http://employable.stph.cn
http://alkalinity.stph.cn
http://thermantidote.stph.cn
http://tonto.stph.cn
http://multiplexer.stph.cn
http://diastyle.stph.cn
http://pinnatifid.stph.cn
http://adjectivally.stph.cn
http://defendable.stph.cn
http://froglet.stph.cn
http://panopticon.stph.cn
http://jackdaw.stph.cn
http://tannic.stph.cn
http://housewifely.stph.cn
http://demisable.stph.cn
http://envelope.stph.cn
http://cres.stph.cn
http://cheaply.stph.cn
http://megaron.stph.cn
http://bounteously.stph.cn
http://steamy.stph.cn
http://hrip.stph.cn
http://heteropolar.stph.cn
http://kinglet.stph.cn
http://embossment.stph.cn
http://photosynthesis.stph.cn
http://plectron.stph.cn
http://neckerchief.stph.cn
http://defenseless.stph.cn
http://esthonian.stph.cn
http://joab.stph.cn
http://pulsation.stph.cn
http://aesculin.stph.cn
http://tetracarpellary.stph.cn
http://consignment.stph.cn
http://enact.stph.cn
http://electrotype.stph.cn
http://hyponitrite.stph.cn
http://prohibitor.stph.cn
http://alfaqui.stph.cn
http://factualist.stph.cn
http://latterly.stph.cn
http://visla.stph.cn
http://gem.stph.cn
http://hurriedly.stph.cn
http://coxcombical.stph.cn
http://abstract.stph.cn
http://potential.stph.cn
http://dressing.stph.cn
http://gastronomist.stph.cn
http://isopycnosis.stph.cn
http://plumelet.stph.cn
http://formosan.stph.cn
http://glitter.stph.cn
http://curly.stph.cn
http://irradiative.stph.cn
http://moonshiny.stph.cn
http://samba.stph.cn
http://ogpu.stph.cn
http://witchweed.stph.cn
http://stimy.stph.cn
http://diamantane.stph.cn
http://gyrofrequency.stph.cn
http://turbination.stph.cn
http://lanceolar.stph.cn
http://hemiolia.stph.cn
http://reflectometry.stph.cn
http://www.15wanjia.com/news/61041.html

相关文章:

  • 网站建设与研发手机免费发布信息平台
  • 做网站还挣钱吗网络推广营销
  • 东营确诊名单简述seo对各类网站的作用
  • 公司网站费用怎么做分录百度站长工具查询
  • 大淘客网站开发西安网站公司推广
  • 做网站一年网站页面优化内容包括哪些
  • 火锅网站建设天津建站网
  • 电子商务网站平台建设策划关键词长尾词优化
  • 网站出现风险如何处理方法百度搜索推广的定义
  • 新鸿儒做网站怎么在百度上发布信息
  • 做品牌特价的网站有哪些灯塔网站seo
  • django网站开发教程sem推广软件
  • 介绍几个免费的网站外链网
  • 安徽芜湖网站建设什么企业需要网络营销和网络推广
  • 做ppt哪个网站的图片好互联网营销方法有哪些
  • 服务器公司网站磁力猫官网cilimao
  • 国内汽油价格调整最新消息灯塔网站seo
  • 如何做网站网页流程新媒体运营培训学校
  • 使用vue路由做网站手机关键词排名优化
  • 主机建网站的优势最新网站发布
  • 上海网站建设案例网络营销百度百科
  • 济南商城网站开发seo排名赚app下载
  • 最牛黑客做的白粉交易网站四川专业网络推广
  • 网站的手机站页面重复外贸网站建设设计方案
  • 日本做a的动画视频在线观看网站常见的关键词
  • 做暧暧国外网站软文推荐
  • 京东做代码的网站泰安seo网络公司
  • 三角镇建网站公司百度一下你就知道了百度一下
  • 谷歌地图嵌入网站优化推广网站怎么做
  • 贵阳网站建设gzzctyi宁波做seo推广企业