当前位置:网站首页>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
边栏推荐
- Jerry's wif interferes with Bluetooth [chapter]
- BERT为何无法彻底干掉BM25??
- 真香啊!最全的 Pycharm 常用快捷键大全!
- Mysql database literacy, do you really know what a database is
- 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
- Jerry's wif interferes with Bluetooth [chapter]
- Inftnews | technology giants accelerate their march into Web3 and metauniverse
- [机缘参悟-32]:鬼谷子-抵巇[xī]篇-面对危险与问题的五种态度
- php获取数字的个位数并替换为指定的尾数
- Matlab plotyy coordinate axis setting, [reprint] example of MATLAB plotyy drawing double ordinate graph [easy to understand]
猜你喜欢

Pytorch model

Commonly used "redmine" for # test bug

From PDB source code to frame frame object

FS7022方案系列FS4059A双节两节锂电池串联充电IC和保护IC

Hubble数据库x某股份制商业银行:冠字号码管理系统升级,让每一张人民币都有 “身份证”

The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?

Visual design tutorial of word cloud

程序员坐牢了,会被安排去写代码吗?

First knowledge of exception

药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计
随机推荐
How vscade sets auto save code
FS7022方案系列FS4059A双节两节锂电池串联充电IC和保护IC
Mature case and source code of hash quiz game system development technology
Yii2 connects to websocket service to realize that the server actively pushes messages to the client
Oracle cloud infrastructure extends distributed cloud services to provide organizations with greater flexibility and controllability
Hisilicon 35xx realizes gt911 touch screen function "suggestions collection"
嵌入式设计与开发项目-液位检测告警系统
Visual design tutorial of word cloud
thinkphp6 多级控制器目录访问解决方法
RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)
5A synchronous rectifier chip 20V to 12v2a/5v4.5a high current 24W high power synchronous rectifier chip high current step-down IC fs2462
Jerry's wif interferes with Bluetooth [chapter]
How to display the server list of the electric donkey, and how to update the eMule server list
En parlant d'exception - que se passe - t - il lorsque l'exception est lancée?
Template_ Large integer multiplication
其他国产手机未能填补华为的空缺,苹果在高端手机市场已无对手
My hematemesis collection integrates script teaching from various classic shell books. As Xiaobai, come quickly
再谈exception——异常抛出时会发生什么?
ThreadLocal的简单理解
Is it safe for Huatai Securities to open an account? Is there any risk in opening an account