当前位置:网站首页>go数组与切片,[]byte转string[通俗易懂]
go数组与切片,[]byte转string[通俗易懂]
2022-06-28 13:37:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
数组:同一种数据类型的固定长度序列 数组的定义:var a [len]int ,比如 var a[5]int 长度是数组类型的一部分,因此,var a [5]int 和 var a [10]int 是不同的类型
func Array() {
//数组的初始化
var age0 [5]int = [5]int{1,2,3}
var age1 = [5]int{1,2,3,4,5}
var age2 = [...]int{1,2,3,4,5,6}
var str = [5]string{3:"hello world",4:"john"} //指定数组的第三个和第四个元素
fmt.Println(age0)
fmt.Println(age1)
fmt.Println(age2)
fmt.Println(str)
}
func DemiArray() {
//多维数组 定义一个三行四列的二维数组
var a [3][4]int = [...][4]int{
{1,2,3,4},{5,6,7,8},{9,10,11,12}}
for _,v:= range a{
for _,v1 := range v{
fmt.Printf("%d\t",v1)
}
fmt.Printf("\n")
}
}
func main() {
Array()
DemiArray()
}切片:切片是数组的一个引用,因此切片是引用类型的 切片的长度是可变的,因此切片是一个可变的数组 切片的遍历方式和数组一样,可以用len()求长度 cap可以求出slice的最大容量,0 <= len(slice) <= cap(array),其中array是slice引用的数组 切片的定义:var 变量名 []类型,比如 var str []string var arr []int
func silce() {
//定义一个数组从数组里切出切片
var a = [10]int{1,2,3,4}
b := a[1:5]
fmt.Println(b)
//定义切片,并用make函数初始
var s = []int
s = make([]int,5)
s[0] = 1
fmt.Println(s)
//一个切片加另一个切片
var c = []int{1,2,3}
var d = []int{4,5,6,7}
c = append(c,d...) //切片append切片
fmt.Println(c)
//切片的拷贝,内置函数copy
s1 :=[]int{1,2,3,4,5}
s2 := make([]int,10)
copy(s2,s1)
fmt.Printf("s2:%d\n",s2)
}[]byte 互转string s1 := “hello,word” s2 :=[]byte(s1) //string转[]byte s1 = string(s2) //[]byte转string
func modifystring () {
//slice与string的关系
/*string本身是不可变的,因此要改变string中的字符串,需要如下操作*/
str := "hello world"
s := []byte(str)
s[0] = '0'
str = string(s)
fmt.Println(str)
//但是字符串如果是中文转换成byte类型的数组就出错了,应该用
s1 := "我爱你,中国"
s2 := []rune(s1)
s2[0] = '你'
s1 = string(s2)
fmt.Println(s1)
}用slice非递归实现斐波那契数列
func fibo(n int){
//用slice非递归的实现Fibonacci数列
var a [] int64
a = make([]int64,n)
a[0] = 1
a[1] = 1
for i := 2; i < n; i++{
a[i] = a[i-1] +a[i-2]
}
for _,v := range a{
fmt.Println(v,)
}
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/150608.html原文链接:https://javaforall.cn
边栏推荐
- 中国广电5G套餐来了,比三大运营商低,却没预期那么低
- How vscade sets auto save code
- 为什么新的5G标准将为技术栈带来更低的 TCO
- Idea global search shortcut settings
- Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform
- Pytorch model
- Mobile web training day-1
- ThreadLocal的简单理解
- 股票网上开户及开户流程怎样?手机开户是安全么?
- PostgreSQL surpasses MySQL
猜你喜欢

Google Earth engine (GEE) - Global organic soil area of FAO (1992-2018)
![(original) [Maui] realize](/img/76/d79b9cf4ed44870bb20a189315def9.jpg)
(original) [Maui] realize "floating action button" step by step

How to find opportunities in a bear market?

Kubernetes 深入理解kubernetes(一)

FH511+TP4333组成一个户外移动电源照明野营灯方案。

中国广电5G套餐来了,比三大运营商低,却没预期那么低

China Radio and television 5g package is coming, lower than the three major operators, but not as low as expected
![Buuctf:[wustctf2020] plain](/img/0f/a7973d3f7593f2464e48609e27d7bd.png)
Buuctf:[wustctf2020] plain

PHP抓取网页获取特定信息

Special test for cold and hot start of app
随机推荐
Flutter series part: detailed explanation of GridView layout commonly used in flutter
RK3399平台开发系列讲解(使用篇)Pinctrl子系统的介绍 - 视频介绍
First knowledge of exception
Kubernetes in-depth understanding of kubernetes (I)
How vscade sets auto save code
设计人工智能产品:技术可能性、用户合意性、商业可行性
List set to array
To be the Italian Islander? Liuqiangdong cashed out 6.6 billion yuan in two months and made a one-time 560million "emergency transfer" to buy the European maritime Palace
En parlant d'exception - que se passe - t - il lorsque l'exception est lancée?
Forecast and Analysis on market scale and development trend of China's operation and maintenance security products in 2022
哈希竞猜游戏系统开发技术成熟案例及源码
How to open an account on the flush? Is it safe
Buuctf:[wustctf2020] plain
为什么新的5G标准将为技术栈带来更低的 TCO
(原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
New product experience: Alibaba cloud's new generation of local SSD instance I4 open beta
Unit test ci/cd
中国广电5G套餐来了,比三大运营商低,却没预期那么低
Rk3399 platform development series explanation (use part) pinctrl subsystem introduction - Video Introduction
股票网上开户及开户流程怎样?手机开户是安全么?